2017-04-30 5 views
6

私はReact-reduxアプリケーションの開発にはとても新しいので、ページがロードされるとすぐに別のアクションをディスパッチする方法を理解しようとしています。以下は私のコンテナコードです。私はこの(https://github.com/jpsierens/webpack-react-redux)定型文を使用しています。 SETSTATE(...):私はレンダリング機能(IDK)の間にアクションを派遣していますので、おそらく既存の状態遷移中に更新できませんレンダリングコンテナの前にレスキューレスキューディスパッチアクションを実行する

let locationSearch; 

const ActivationPage = ({activateUser}) => { 
    return (
     <div className="container"> 
      <h2>Activation Required</h2> 
      <p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p> 
      { activateUser() } 
     </div> 
    ); 
}; 


ActivationPage.propTypes = { 
    activateUser: PropTypes.func 
}; 


const mapStateToProps = (state) => { 
    return { 
     message: state.message, 
     currentUser: state.currentUser, 
     request: state.request 
    }; 
}; 

const mapDispatchToProps = (dispatch) => { 
    return { 
     activateUser:() => { 
      console.log(location); 
      if (location.search !== '') { 
       locationSearch = querystring.parse(location.search.replace('?', '')); 
       console.log(locationSearch); 
       if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) { 
        // change request state to pending to show loader 
        dispatch(actions.requestActions.requestPending()); 

       } 
      } 
     } 
    }; 
}; 

export default connect(
    mapStateToProps, 
    mapDispatchToProps 
)(ActivationPage); 

このコードは、私に警告を与えます。どのようにすれば、ページがロードされると自動的にactivateUser()関数をトリガーするように、指定されたコードを変換できますか?あなたのための

答えて

4

https://facebook.github.io/react/docs/react-component.html

componentWillMount()componentDidMount()。 そして、私の意見 - あなたはcomponentWillMountを使用して回避しcomponentDidMountを好む必要があります - これはあなたがsomewhen

+0

@Saad Anjumこの回答に記載されているライフサイクル機能を使用するには、 'React.Component'を拡張するために' ActivationPage'を変更する必要があります。 –

+0

正しい方向に私を押してくれてありがとう@MichaelPeyperとYozi。それは今働いている。 –

2

をレンダリングするサーバーを使用する場合は理にかなっている、私はこの答えはここに助けextends Componentclassするコンポーネントを変換した後、それが働いて手に入れました私はいくつかの概念を持っています How do you mix componentDidMount() with react-redux connect()? これは私のようにこれについて混乱するかもしれない人々のための実装です。

関連する問題