ComponentWillReceiveProps is not called when we navigate between stack navigator components?











up vote
0
down vote

favorite












export default (DrawNav = createStackNavigator(
{
Home: { screen: Home },
QuestionDetail: { screen: QuestionDetail },
QuestionAsk: { screen: QuestionAsk }
},
{
initialRouteName: "Home",
headerMode: "none"
}
));


Home component lists questions and QuestionDetail shows detail information of the questions but here is the problem that i faced, whenever you back to home from QuestionDetail or other component i want to grab the questions and here is what i did in Home component,



componentDidMount() {
this.getQuestions();
}

componentWillReceiveProps() {
this.setState({ questions: }, () => {
this.getQuestions();
});
}

getQuestions() {
this.setState({ isLoading: true });
axios.get(`http://${IP_ADDRESS}/api/questions`)
.then(response => {
console.log('response data: ', response.data);
this.setState({ questions: response.data, isLoading: false })
})
.catch((err) => {
this.setState({ isLoading: false });
console.log('QUESTIONS ERR: '+err);
// this.props.history.push('/');
})
}


but componentWillReceiveProps is not called when you navigate from QuestionDetail to Home?










share|improve this question


























    up vote
    0
    down vote

    favorite












    export default (DrawNav = createStackNavigator(
    {
    Home: { screen: Home },
    QuestionDetail: { screen: QuestionDetail },
    QuestionAsk: { screen: QuestionAsk }
    },
    {
    initialRouteName: "Home",
    headerMode: "none"
    }
    ));


    Home component lists questions and QuestionDetail shows detail information of the questions but here is the problem that i faced, whenever you back to home from QuestionDetail or other component i want to grab the questions and here is what i did in Home component,



    componentDidMount() {
    this.getQuestions();
    }

    componentWillReceiveProps() {
    this.setState({ questions: }, () => {
    this.getQuestions();
    });
    }

    getQuestions() {
    this.setState({ isLoading: true });
    axios.get(`http://${IP_ADDRESS}/api/questions`)
    .then(response => {
    console.log('response data: ', response.data);
    this.setState({ questions: response.data, isLoading: false })
    })
    .catch((err) => {
    this.setState({ isLoading: false });
    console.log('QUESTIONS ERR: '+err);
    // this.props.history.push('/');
    })
    }


    but componentWillReceiveProps is not called when you navigate from QuestionDetail to Home?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      export default (DrawNav = createStackNavigator(
      {
      Home: { screen: Home },
      QuestionDetail: { screen: QuestionDetail },
      QuestionAsk: { screen: QuestionAsk }
      },
      {
      initialRouteName: "Home",
      headerMode: "none"
      }
      ));


      Home component lists questions and QuestionDetail shows detail information of the questions but here is the problem that i faced, whenever you back to home from QuestionDetail or other component i want to grab the questions and here is what i did in Home component,



      componentDidMount() {
      this.getQuestions();
      }

      componentWillReceiveProps() {
      this.setState({ questions: }, () => {
      this.getQuestions();
      });
      }

      getQuestions() {
      this.setState({ isLoading: true });
      axios.get(`http://${IP_ADDRESS}/api/questions`)
      .then(response => {
      console.log('response data: ', response.data);
      this.setState({ questions: response.data, isLoading: false })
      })
      .catch((err) => {
      this.setState({ isLoading: false });
      console.log('QUESTIONS ERR: '+err);
      // this.props.history.push('/');
      })
      }


      but componentWillReceiveProps is not called when you navigate from QuestionDetail to Home?










      share|improve this question













      export default (DrawNav = createStackNavigator(
      {
      Home: { screen: Home },
      QuestionDetail: { screen: QuestionDetail },
      QuestionAsk: { screen: QuestionAsk }
      },
      {
      initialRouteName: "Home",
      headerMode: "none"
      }
      ));


      Home component lists questions and QuestionDetail shows detail information of the questions but here is the problem that i faced, whenever you back to home from QuestionDetail or other component i want to grab the questions and here is what i did in Home component,



      componentDidMount() {
      this.getQuestions();
      }

      componentWillReceiveProps() {
      this.setState({ questions: }, () => {
      this.getQuestions();
      });
      }

      getQuestions() {
      this.setState({ isLoading: true });
      axios.get(`http://${IP_ADDRESS}/api/questions`)
      .then(response => {
      console.log('response data: ', response.data);
      this.setState({ questions: response.data, isLoading: false })
      })
      .catch((err) => {
      this.setState({ isLoading: false });
      console.log('QUESTIONS ERR: '+err);
      // this.props.history.push('/');
      })
      }


      but componentWillReceiveProps is not called when you navigate from QuestionDetail to Home?







      reactjs react-native react-navigation react-navigation-stack






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 9:00









      Henok Tesfaye

      343215




      343215
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          componentWillReceiveProps is triggered only when component prop updates and not on initial render. As the documentation states,




          React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().




          componentWillReceiveProps is deprecated, particularly because it's often misused. For asynchronous actions componentDidMount and componentDidUpdate are supposed to be used instead of componentWillMount and componentWillReceiveProps:




          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.




          If same logic is applicable to both hooks, there should be a method to reuse. There's already such method, getQuestions:



          componentDidMount() {
          this.getQuestions();
          }

          componentDidUpdate() {
          this.getQuestions();
          }

          getQuestions() {
          this.setState({ isLoading: true, questions: });

          axios.get(`http://${IP_ADDRESS}/api/questions`)
          ...
          }





          share|improve this answer





















          • Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
            – ThaJay
            Nov 7 at 10:27










          • example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
            – ThaJay
            Nov 7 at 10:34












          • @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
            – estus
            Nov 7 at 11:14










          • @estus componentDidUpdate is called many times.
            – Henok Tesfaye
            Nov 7 at 18:06






          • 1




            @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
            – estus
            Nov 7 at 19:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186233%2fcomponentwillreceiveprops-is-not-called-when-we-navigate-between-stack-navigator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          componentWillReceiveProps is triggered only when component prop updates and not on initial render. As the documentation states,




          React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().




          componentWillReceiveProps is deprecated, particularly because it's often misused. For asynchronous actions componentDidMount and componentDidUpdate are supposed to be used instead of componentWillMount and componentWillReceiveProps:




          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.




          If same logic is applicable to both hooks, there should be a method to reuse. There's already such method, getQuestions:



          componentDidMount() {
          this.getQuestions();
          }

          componentDidUpdate() {
          this.getQuestions();
          }

          getQuestions() {
          this.setState({ isLoading: true, questions: });

          axios.get(`http://${IP_ADDRESS}/api/questions`)
          ...
          }





          share|improve this answer





















          • Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
            – ThaJay
            Nov 7 at 10:27










          • example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
            – ThaJay
            Nov 7 at 10:34












          • @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
            – estus
            Nov 7 at 11:14










          • @estus componentDidUpdate is called many times.
            – Henok Tesfaye
            Nov 7 at 18:06






          • 1




            @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
            – estus
            Nov 7 at 19:48















          up vote
          0
          down vote













          componentWillReceiveProps is triggered only when component prop updates and not on initial render. As the documentation states,




          React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().




          componentWillReceiveProps is deprecated, particularly because it's often misused. For asynchronous actions componentDidMount and componentDidUpdate are supposed to be used instead of componentWillMount and componentWillReceiveProps:




          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.




          If same logic is applicable to both hooks, there should be a method to reuse. There's already such method, getQuestions:



          componentDidMount() {
          this.getQuestions();
          }

          componentDidUpdate() {
          this.getQuestions();
          }

          getQuestions() {
          this.setState({ isLoading: true, questions: });

          axios.get(`http://${IP_ADDRESS}/api/questions`)
          ...
          }





          share|improve this answer





















          • Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
            – ThaJay
            Nov 7 at 10:27










          • example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
            – ThaJay
            Nov 7 at 10:34












          • @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
            – estus
            Nov 7 at 11:14










          • @estus componentDidUpdate is called many times.
            – Henok Tesfaye
            Nov 7 at 18:06






          • 1




            @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
            – estus
            Nov 7 at 19:48













          up vote
          0
          down vote










          up vote
          0
          down vote









          componentWillReceiveProps is triggered only when component prop updates and not on initial render. As the documentation states,




          React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().




          componentWillReceiveProps is deprecated, particularly because it's often misused. For asynchronous actions componentDidMount and componentDidUpdate are supposed to be used instead of componentWillMount and componentWillReceiveProps:




          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.




          If same logic is applicable to both hooks, there should be a method to reuse. There's already such method, getQuestions:



          componentDidMount() {
          this.getQuestions();
          }

          componentDidUpdate() {
          this.getQuestions();
          }

          getQuestions() {
          this.setState({ isLoading: true, questions: });

          axios.get(`http://${IP_ADDRESS}/api/questions`)
          ...
          }





          share|improve this answer












          componentWillReceiveProps is triggered only when component prop updates and not on initial render. As the documentation states,




          React doesn’t call UNSAFE_componentWillReceiveProps() with initial props during mounting. It only calls this method if some of component’s props may update. Calling this.setState() generally doesn’t trigger UNSAFE_componentWillReceiveProps().




          componentWillReceiveProps is deprecated, particularly because it's often misused. For asynchronous actions componentDidMount and componentDidUpdate are supposed to be used instead of componentWillMount and componentWillReceiveProps:




          If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.




          If same logic is applicable to both hooks, there should be a method to reuse. There's already such method, getQuestions:



          componentDidMount() {
          this.getQuestions();
          }

          componentDidUpdate() {
          this.getQuestions();
          }

          getQuestions() {
          this.setState({ isLoading: true, questions: });

          axios.get(`http://${IP_ADDRESS}/api/questions`)
          ...
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 9:15









          estus

          62.8k2193200




          62.8k2193200












          • Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
            – ThaJay
            Nov 7 at 10:27










          • example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
            – ThaJay
            Nov 7 at 10:34












          • @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
            – estus
            Nov 7 at 11:14










          • @estus componentDidUpdate is called many times.
            – Henok Tesfaye
            Nov 7 at 18:06






          • 1




            @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
            – estus
            Nov 7 at 19:48


















          • Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
            – ThaJay
            Nov 7 at 10:27










          • example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
            – ThaJay
            Nov 7 at 10:34












          • @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
            – estus
            Nov 7 at 11:14










          • @estus componentDidUpdate is called many times.
            – Henok Tesfaye
            Nov 7 at 18:06






          • 1




            @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
            – estus
            Nov 7 at 19:48
















          Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
          – ThaJay
          Nov 7 at 10:27




          Also, static getDerivedStateFromProps if you want to derive some state from new props without rendering first.
          – ThaJay
          Nov 7 at 10:27












          example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
          – ThaJay
          Nov 7 at 10:34






          example: (excuse the bad formatting in comments) static getDerivedStateFromProps (props, state) { if (props.drawerOpen !== state.drawerOpen || props.width !== state.width) { return { drawerOpen: props.drawerOpen, width: props.width, landscape: getLandscape(props) } } else return null }
          – ThaJay
          Nov 7 at 10:34














          @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
          – estus
          Nov 7 at 11:14




          @ThaJay I believe getDerivedStateFromProps is not really applicable here because this isn't a place for side effects (async request is a side effect), also it couldn't get access to this.getQuestions,
          – estus
          Nov 7 at 11:14












          @estus componentDidUpdate is called many times.
          – Henok Tesfaye
          Nov 7 at 18:06




          @estus componentDidUpdate is called many times.
          – Henok Tesfaye
          Nov 7 at 18:06




          1




          1




          @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
          – estus
          Nov 7 at 19:48




          @HenokTesfaye I see what you mean with 'componentDidUpdate is called many times'. I guess you've got recursive state updates. it is not recommended to call setState inside componentDidUpdate. - there's no such recommendation. It's ok to call setState there. You just need to prevent recursive state updates. This is what But updates should be controlled with shouldComponentUpdate or PureComponent is about. Which updates should make getQuestions run? Are there problems with using PureComponent?
          – estus
          Nov 7 at 19:48


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186233%2fcomponentwillreceiveprops-is-not-called-when-we-navigate-between-stack-navigator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini