2017-07-18 266 views
2

我有這樣的反應組件與路由器。如果通過道具傳遞的登錄頁面是錯誤的,那麼這將呈現登錄頁面道具是由redux狀態推動的。React路由器在狀態改變後不重新渲染

const Root = ({logged, history}) => { 
    console.log(logged); 
    return (
    <div className="App"> 
     <Router history={history}> 
     <div> 
      <Route exact path="/" component={ATM} /> 
      <Route path="/login" component={LoginForm}/> 
     </div> 
     </Router> 
     //Removed without effect-->{!logged ? <Redirect to="/login"/> : ""} 
    </div> 
) 
}; 

const fromState = (state) => ({ 
    logged: state.auth.logged 
}); 

export default connect(fromState)(Root) 

在我的登錄組件中,我已經黑了一個調用,要求服務器用戶是否已成功驗證並重定向,如果是這種情況。

this.props.dispatch(action).then((result) => { 
    let responseCode = result.payload.status; 
    if(responseCode === 200) { 
    this.props.dispatch(push("/")) 
    } 
}) 

在我的應用我已經定義了歷史,比如:

let blankHistory = createHistory(); 
let store = storeCreator(blankHistory); 
const history = syncHistoryWithStore(blankHistory,store); 

商店創造者適用中間件和減速器,採用歷史作爲一個參數,因爲routerMiddleware需要它。

之後,被稱爲狀態正在改變,日誌成爲true,我的瀏覽器中的路徑正在改變,歷史對象知道該路徑的變化,但我仍然看到呈現登錄組件和console.log(logged)不被稱爲第二次。

有誰知道究竟是什麼問題?

- 編輯 - 後位的調查,我添加此位代碼初始化:

let history = syncHistoryWithStore(blankHistory,store); 
console.log(history.location) 
history.listen((something) => { 
    console.log(something) 
}); 

和歷史中第一個日誌具有正確的路徑,但歷史後immedietaly接收路徑:「/」這可能是問題的原因。

+0

我能看到的第一個問題是在''組件之外有''。嘗試在''內移動它,然後檢查是否仍有問題。 –

+0

我仍然有問題,如果我刪除該重定向 – Haito

回答

2

問題出在react-redux-routerreact-router的不兼容版本上。我已經更新了第一個,它的作用就像一個魅力。

相關問題