0

在 react-router v2 和 v3 中,我们可以做

import React, { Component, PropTypes } from "react";

class PostsNew extends Component {

  static contextTypes = {
    router: PropTypes.object
  }

  handleUserSubmit(props) {
    this.props.createPost(props)
      .then(() => {
        // the post has been created successfully
        // now route back to index
        this.context.router.push("/");
      });
  }

  // ...

但是在 react-router v4 中,如果我们需要做的就是this.props.history.push()而不是使用context

  handleUserSubmit(props) {
    this.props.createPost(props)
      .then(() => {
        // the post has been created successfully
        // now route back to index
        this.props.history.push("/");
      });
  }

那么我们可以跳过 的导入PropTypes和定义contextTypes吗?所以改变路径就像this.props.history.push("/");我们使用 react-router v4 一样简单?

4

1 回答 1

0

是的。或者您可以使用重定向组件。

于 2017-04-16T00:48:04.950 回答