2017-07-17 47 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> 
    ); 
} 

你的消息应该清楚什么时候位置的变化。

我建议创建一个专门用于此任务的组件,然后您可以将其包含在您期望消息的任何组件中。