2017-07-17 91 views
0

每當我改變路線時,我都會通過位置狀態發送消息。 我想在componentWillUnmont()時刪除該消息。刪除當前位置的狀態

`this.context.router.push({ 
      pathname: '/EmpReferralListView', 
      state: { 
       message: (typeof message != 'undefined') ? message : '' 
      } 
    });` 

回答

1

對於任何人停留在一個類似的問題,好像我是......這是反應路由器V4。

你可以做什麼,取而代之的是鉤入componentWillMount()方法。

在組件的構造函數定義message

constructor(props) { 
    super(props); 
    this.message = ''; 
} 

然後你componentWillMount()看起來就像這樣:

componentWillMount() { 
    if (this.props.location.state && this.props.location.state.message) { 
     this.message = this.props.location.state.message; 
     this.props.history.replace({ 
      pathname: this.props.location.pathname, 
      state: {} 
     }); 
    } 
} 

上面的代碼將取代位置的狀態你已經抓住了消息之後。因此,您需要使用this.message來顯示正在前進的消息。現在

,你可以參考你的信息呈現,像這樣在:

render() { 
    return (
     <div>{this.message}</div> 
    ); 
} 

你的消息應該清楚什麼時候位置的變化。

我建議創建一個專門用於此任務的組件,然後您可以將其包含在您期望消息的任何組件中。