2017-04-14 17 views
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("/");一樣簡單?

回答

0

是的。或者您可以使用Redirect組件。

+0

你的意思是「是的,我們仍然需要使用'PropTypes'和'contextTypes'」或者「是的,那麼我們不需要使用'PropTypes'或'contextTypes'」? –

+0

我們可以跳過PropTypes的導入和contextTypes的定義嗎?是。 –

相關問題