在 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 一样简单?