2017-02-23 45 views
0

我的應用程序是使用react + redux,我用this.context.router API來重定向我的頁面,但是反應官方建議我們不需要用戶使用context API,而且我也用history來重定向頁面,然後我會收到來自console的警告「警告:[react-router] props.history和context.history已棄用,請使用context.router。」,那麼如何使用js代碼重定向如何使用js代碼重定向

+0

是否使用的是'反應,router'的版本? – jakee

回答

0

這取決於你的react-router

版本在v4你能想到的redirection as a component

class MyComponent extends Component { 
    render() { 
    return this.props.isLoggedIn ? (
     <p>You're logged in!!</p> 
    ) : (
     <Redirect to="/login" /> 
    ) 
    } 
} 
v3

我會建議使用browserHistory

import { browserHistory } from 'react-router' 

class MyComponent extends Component { 
    componentDidMount() { 
    if (!this.props.isLoggedIn) browserHistory.replace('/login') 
    } 

    render() { 
    if (this.props.isLoggedIn) return (<p>You're logged in!!</p>) 
    return null 
    } 
} 
+0

謝謝,我想用js代碼重定向尋呼機,比如'browserHistory.replace',絕對使用''也可以重定向,但是我不能使用標籤,所以我使用'history' API,但是我會得到警告'警告:[react-router] props.history和context.history已被棄用。請使用context.router' – taven