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?
reactjs react-native react-navigation react-navigation-stack
add a comment |
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?
reactjs react-native react-navigation react-navigation-stack
add a comment |
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?
reactjs react-native react-navigation react-navigation-stack
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
reactjs react-native react-navigation react-navigation-stack
asked Nov 7 at 9:00
Henok Tesfaye
343215
343215
add a comment |
add a comment |
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`)
...
}
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 makegetQuestions
run? Are there problems with using PureComponent?
– estus
Nov 7 at 19:48
|
show 5 more comments
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`)
...
}
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 makegetQuestions
run? Are there problems with using PureComponent?
– estus
Nov 7 at 19:48
|
show 5 more comments
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`)
...
}
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 makegetQuestions
run? Are there problems with using PureComponent?
– estus
Nov 7 at 19:48
|
show 5 more comments
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`)
...
}
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`)
...
}
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 makegetQuestions
run? Are there problems with using PureComponent?
– estus
Nov 7 at 19:48
|
show 5 more comments
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 makegetQuestions
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
|
show 5 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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