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

あなたのメッセージは時に場所の変更をクリアする必要があります。

このタスク専用のコンポーネントを作成することをお勧めします。このコンポーネントは、メッセージが必要なコンポーネントに含めることができます。

関連する問題