0
在反應路由器v2和v3,我們可以做我們是否需要在解決承諾後更改路徑時導入PropTypes並定義contextTypes?
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("/");
});
}
// ...
但在反應路由器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
的定義?如果我們使用react-router v4,那麼改變路徑將會如同this.props.history.push("/");
一樣簡單?
你的意思是「是的,我們仍然需要使用'PropTypes'和'contextTypes'」或者「是的,那麼我們不需要使用'PropTypes'或'contextTypes'」? –
我們可以跳過PropTypes的導入和contextTypes的定義嗎?是。 –