2017-02-24 68 views
0

我試圖重定向用戶從頁面到同一頁面與用戶登錄後查詢參數buy=true。用戶登錄後在我的服務響應處理程序中,我試圖使用react-router-redux中的推送功能來推送狀態。這沒有奏效。功能replace也沒有。新網址已更新,但我的組件未再次呈現。什麼是正確的方法呢?推送一個新的網址,其中唯一的變化與前一個網址是查詢參數

這是我試過

dispatch(push(/home?buy=true)) 
dispatch(replace(/home?buy=true)) 
browserHistory.push(/home?buy=true) 
+0

你究竟是如何嘗試使用'push'和'replace'發現它沒有工作?請詳細提及您已經嘗試過的內容。只是說功能的名稱不起作用。 – Lucas

回答

0

我想你可以嘗試這樣的做法:

  • 創建一個本地狀態,使你想要的元素重新渲染

  • Inside ComponentWillReceiveProps or ComponentDidUpdate,你應該tr igget一個的setState改變當前的狀態

類似的東西:

constructor() { 

this.state = { 
    hasRefreshed: false 
} 

componentWillReceiveProps(nextProps) { 
    const paramsHaveChanged = nextProps.location.query !== this.props.location.query 
    if (paramsHaveChanged) { 
    this.setState({ hasRefreshed: true }) 
    } 
    if (this.state.hasRefreshed) { 
    this.setState({ hasFrefreshed: false }) 
    } 
} 

render() { 

    if (hasRefreshed) return null 
    return() { 
    //other elements 
    } 
} 
相關問題