react-native How to listen to updated mobx state
In my react native project, when I click a button, my mobx app state gets updated.
I want to use react lifecycle method
to listen to this update and automatically update it. So I am using componentDidUpdate
.
componentDidUpdate(nextProps, nextState) {
this.setState({number:store.requestObject.number},
console.log(this.state.number), 'state changed')
}
But it seems like this method will not automatically update.
Should I be using other lifecycle method? Any comments or advise would be really helpful. Thanks in advance!
Edited
In my first screen, I have a button and onPress
I can store data to mobx
store
. Then, in my second screen, I want to call the mobx
state
and update it with my current component's state. I could use button something like:
<Button onPress={()=> this.setState({currentState:store.mobxState})}>
Then, my current component state
will match the mobx state
it works fine but I want to do it automatically(not with a button).
javascript reactjs react-native mobx
|
show 5 more comments
In my react native project, when I click a button, my mobx app state gets updated.
I want to use react lifecycle method
to listen to this update and automatically update it. So I am using componentDidUpdate
.
componentDidUpdate(nextProps, nextState) {
this.setState({number:store.requestObject.number},
console.log(this.state.number), 'state changed')
}
But it seems like this method will not automatically update.
Should I be using other lifecycle method? Any comments or advise would be really helpful. Thanks in advance!
Edited
In my first screen, I have a button and onPress
I can store data to mobx
store
. Then, in my second screen, I want to call the mobx
state
and update it with my current component's state. I could use button something like:
<Button onPress={()=> this.setState({currentState:store.mobxState})}>
Then, my current component state
will match the mobx state
it works fine but I want to do it automatically(not with a button).
javascript reactjs react-native mobx
1
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
What isstore.requestObject.number
in your example? Where does it come from? As is, ifcomponentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unlessstore
orrequestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?
– Drew Reese
Jan 2 at 6:52
1
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the reactsetState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.
– Drew Reese
Jan 2 at 7:22
|
show 5 more comments
In my react native project, when I click a button, my mobx app state gets updated.
I want to use react lifecycle method
to listen to this update and automatically update it. So I am using componentDidUpdate
.
componentDidUpdate(nextProps, nextState) {
this.setState({number:store.requestObject.number},
console.log(this.state.number), 'state changed')
}
But it seems like this method will not automatically update.
Should I be using other lifecycle method? Any comments or advise would be really helpful. Thanks in advance!
Edited
In my first screen, I have a button and onPress
I can store data to mobx
store
. Then, in my second screen, I want to call the mobx
state
and update it with my current component's state. I could use button something like:
<Button onPress={()=> this.setState({currentState:store.mobxState})}>
Then, my current component state
will match the mobx state
it works fine but I want to do it automatically(not with a button).
javascript reactjs react-native mobx
In my react native project, when I click a button, my mobx app state gets updated.
I want to use react lifecycle method
to listen to this update and automatically update it. So I am using componentDidUpdate
.
componentDidUpdate(nextProps, nextState) {
this.setState({number:store.requestObject.number},
console.log(this.state.number), 'state changed')
}
But it seems like this method will not automatically update.
Should I be using other lifecycle method? Any comments or advise would be really helpful. Thanks in advance!
Edited
In my first screen, I have a button and onPress
I can store data to mobx
store
. Then, in my second screen, I want to call the mobx
state
and update it with my current component's state. I could use button something like:
<Button onPress={()=> this.setState({currentState:store.mobxState})}>
Then, my current component state
will match the mobx state
it works fine but I want to do it automatically(not with a button).
javascript reactjs react-native mobx
javascript reactjs react-native mobx
edited Jan 2 at 7:36
kirimi
asked Jan 2 at 3:04
kirimikirimi
245113
245113
1
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
What isstore.requestObject.number
in your example? Where does it come from? As is, ifcomponentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unlessstore
orrequestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?
– Drew Reese
Jan 2 at 6:52
1
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the reactsetState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.
– Drew Reese
Jan 2 at 7:22
|
show 5 more comments
1
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
What isstore.requestObject.number
in your example? Where does it come from? As is, ifcomponentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unlessstore
orrequestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?
– Drew Reese
Jan 2 at 6:52
1
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the reactsetState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.
– Drew Reese
Jan 2 at 7:22
1
1
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
What is
store.requestObject.number
in your example? Where does it come from? As is, if componentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unless store
or requestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?– Drew Reese
Jan 2 at 6:52
What is
store.requestObject.number
in your example? Where does it come from? As is, if componentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unless store
or requestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?– Drew Reese
Jan 2 at 6:52
1
1
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the react
setState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.– Drew Reese
Jan 2 at 7:22
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the react
setState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.– Drew Reese
Jan 2 at 7:22
|
show 5 more comments
4 Answers
4
active
oldest
votes
You will not get changed state value in the next line. We can use the callback method here
handleMonthChange_next = () => {
this.setState({
currentMonth: this.state.currentMonth + 1
}, () => {
this.props.getCalendarData(this.state.currentMonth)
})}
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
add a comment |
You can use getDerivedStateFromProps
static getDerivedStateFromProps(props, state){
}
Note that this method is fired on every render, regardless of the
cause. This is in contrast to UNSAFE_componentWillReceiveProps, which
only fires when the parent causes a re-render and not as a result of a
local setState.
Reference
To be more specific, when you call this.setState({})
, your getDerivedStateFromProps
will be called and from there you can read your changed state value and perform some operations.
thanks for the comment. I never seen this method before. I read the documents but still confusing.static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }
– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
add a comment |
if you dont want to user given methods then make your custom event that listen to state
stateListiner = (state) =>{
this.setState(state,()=>{
//listen to state change after setting it
// add your logic after state update
})
}
// call the state
let givenState = {name:'xyz',visible:false,}
this.stateListiner(givenState) // this will set your state and fire the callback function
add a comment |
Use the react setState
callback:
componentDidUpdate(prevProps, prevState) {
this.setState(
{ number: store.requestObject.number },
() => console.log(this.state.number, 'state changed') // callback will fire after state update
);
}
Note though that setting state within componentDidUpdate
is recommended to be behind a conditional check so to not get your code into an infinite loop. The code above will. Also, the parameters it receives are the previous state and props, not the next. It's only called after state has been updated or the component receives new prop values.
componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
// handle change, log it, api call, call other callback, etc...
}
}
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54000772%2freact-native-how-to-listen-to-updated-mobx-state%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You will not get changed state value in the next line. We can use the callback method here
handleMonthChange_next = () => {
this.setState({
currentMonth: this.state.currentMonth + 1
}, () => {
this.props.getCalendarData(this.state.currentMonth)
})}
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
add a comment |
You will not get changed state value in the next line. We can use the callback method here
handleMonthChange_next = () => {
this.setState({
currentMonth: this.state.currentMonth + 1
}, () => {
this.props.getCalendarData(this.state.currentMonth)
})}
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
add a comment |
You will not get changed state value in the next line. We can use the callback method here
handleMonthChange_next = () => {
this.setState({
currentMonth: this.state.currentMonth + 1
}, () => {
this.props.getCalendarData(this.state.currentMonth)
})}
You will not get changed state value in the next line. We can use the callback method here
handleMonthChange_next = () => {
this.setState({
currentMonth: this.state.currentMonth + 1
}, () => {
this.props.getCalendarData(this.state.currentMonth)
})}
answered Jan 2 at 3:18
Vikram ThakurVikram Thakur
537311
537311
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
add a comment |
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
thanks but I am not sure if we are on the same page. what I am trying to do is listen to the update state and automatically update using the react lifecycle method.
– kirimi
Jan 2 at 4:01
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
render method of react is only lifecycle method which gets called after setState in the same class. What is the use case of listing state change in the same class?
– Vikram Thakur
Jan 2 at 7:21
add a comment |
You can use getDerivedStateFromProps
static getDerivedStateFromProps(props, state){
}
Note that this method is fired on every render, regardless of the
cause. This is in contrast to UNSAFE_componentWillReceiveProps, which
only fires when the parent causes a re-render and not as a result of a
local setState.
Reference
To be more specific, when you call this.setState({})
, your getDerivedStateFromProps
will be called and from there you can read your changed state value and perform some operations.
thanks for the comment. I never seen this method before. I read the documents but still confusing.static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }
– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
add a comment |
You can use getDerivedStateFromProps
static getDerivedStateFromProps(props, state){
}
Note that this method is fired on every render, regardless of the
cause. This is in contrast to UNSAFE_componentWillReceiveProps, which
only fires when the parent causes a re-render and not as a result of a
local setState.
Reference
To be more specific, when you call this.setState({})
, your getDerivedStateFromProps
will be called and from there you can read your changed state value and perform some operations.
thanks for the comment. I never seen this method before. I read the documents but still confusing.static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }
– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
add a comment |
You can use getDerivedStateFromProps
static getDerivedStateFromProps(props, state){
}
Note that this method is fired on every render, regardless of the
cause. This is in contrast to UNSAFE_componentWillReceiveProps, which
only fires when the parent causes a re-render and not as a result of a
local setState.
Reference
To be more specific, when you call this.setState({})
, your getDerivedStateFromProps
will be called and from there you can read your changed state value and perform some operations.
You can use getDerivedStateFromProps
static getDerivedStateFromProps(props, state){
}
Note that this method is fired on every render, regardless of the
cause. This is in contrast to UNSAFE_componentWillReceiveProps, which
only fires when the parent causes a re-render and not as a result of a
local setState.
Reference
To be more specific, when you call this.setState({})
, your getDerivedStateFromProps
will be called and from there you can read your changed state value and perform some operations.
edited Jan 2 at 4:27
answered Jan 2 at 4:17
Ravi RupareliyaRavi Rupareliya
18.9k1268122
18.9k1268122
thanks for the comment. I never seen this method before. I read the documents but still confusing.static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }
– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
add a comment |
thanks for the comment. I never seen this method before. I read the documents but still confusing.static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }
– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
thanks for the comment. I never seen this method before. I read the documents but still confusing.
static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }– kirimi
Jan 2 at 4:25
thanks for the comment. I never seen this method before. I read the documents but still confusing.
static getDerivedStateFromProps(props, state){ this.setState({number:store.requestObject.number}, console.log(this.state.number), 'state changed')
would not work. }– kirimi
Jan 2 at 4:25
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
It would not work like that, it's purpose is to give you an event whenever there is any change in state or props, so whenever you do setState() or your props gets changed, this method will be called.
– Ravi Rupareliya
Jan 2 at 4:26
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
Could you please provide a example code I would really appreciate it!
– kirimi
Jan 2 at 4:37
1
1
refer this
– Ravi Rupareliya
Jan 2 at 4:40
refer this
– Ravi Rupareliya
Jan 2 at 4:40
add a comment |
if you dont want to user given methods then make your custom event that listen to state
stateListiner = (state) =>{
this.setState(state,()=>{
//listen to state change after setting it
// add your logic after state update
})
}
// call the state
let givenState = {name:'xyz',visible:false,}
this.stateListiner(givenState) // this will set your state and fire the callback function
add a comment |
if you dont want to user given methods then make your custom event that listen to state
stateListiner = (state) =>{
this.setState(state,()=>{
//listen to state change after setting it
// add your logic after state update
})
}
// call the state
let givenState = {name:'xyz',visible:false,}
this.stateListiner(givenState) // this will set your state and fire the callback function
add a comment |
if you dont want to user given methods then make your custom event that listen to state
stateListiner = (state) =>{
this.setState(state,()=>{
//listen to state change after setting it
// add your logic after state update
})
}
// call the state
let givenState = {name:'xyz',visible:false,}
this.stateListiner(givenState) // this will set your state and fire the callback function
if you dont want to user given methods then make your custom event that listen to state
stateListiner = (state) =>{
this.setState(state,()=>{
//listen to state change after setting it
// add your logic after state update
})
}
// call the state
let givenState = {name:'xyz',visible:false,}
this.stateListiner(givenState) // this will set your state and fire the callback function
answered Jan 2 at 5:50
JigarJigar
731316
731316
add a comment |
add a comment |
Use the react setState
callback:
componentDidUpdate(prevProps, prevState) {
this.setState(
{ number: store.requestObject.number },
() => console.log(this.state.number, 'state changed') // callback will fire after state update
);
}
Note though that setting state within componentDidUpdate
is recommended to be behind a conditional check so to not get your code into an infinite loop. The code above will. Also, the parameters it receives are the previous state and props, not the next. It's only called after state has been updated or the component receives new prop values.
componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
// handle change, log it, api call, call other callback, etc...
}
}
add a comment |
Use the react setState
callback:
componentDidUpdate(prevProps, prevState) {
this.setState(
{ number: store.requestObject.number },
() => console.log(this.state.number, 'state changed') // callback will fire after state update
);
}
Note though that setting state within componentDidUpdate
is recommended to be behind a conditional check so to not get your code into an infinite loop. The code above will. Also, the parameters it receives are the previous state and props, not the next. It's only called after state has been updated or the component receives new prop values.
componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
// handle change, log it, api call, call other callback, etc...
}
}
add a comment |
Use the react setState
callback:
componentDidUpdate(prevProps, prevState) {
this.setState(
{ number: store.requestObject.number },
() => console.log(this.state.number, 'state changed') // callback will fire after state update
);
}
Note though that setting state within componentDidUpdate
is recommended to be behind a conditional check so to not get your code into an infinite loop. The code above will. Also, the parameters it receives are the previous state and props, not the next. It's only called after state has been updated or the component receives new prop values.
componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
// handle change, log it, api call, call other callback, etc...
}
}
Use the react setState
callback:
componentDidUpdate(prevProps, prevState) {
this.setState(
{ number: store.requestObject.number },
() => console.log(this.state.number, 'state changed') // callback will fire after state update
);
}
Note though that setting state within componentDidUpdate
is recommended to be behind a conditional check so to not get your code into an infinite loop. The code above will. Also, the parameters it receives are the previous state and props, not the next. It's only called after state has been updated or the component receives new prop values.
componentDidUpdate(prevProps, prevState) {
if (prevProps.value !== this.props.value) {
// handle change, log it, api call, call other callback, etc...
}
}
edited Jan 2 at 6:49
answered Jan 2 at 6:33
Drew ReeseDrew Reese
950211
950211
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f54000772%2freact-native-how-to-listen-to-updated-mobx-state%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
1
You can use setState callback function to get updated state value. And whenever you update your state you can always get it in your render method.
– Vikram Thakur
Jan 2 at 3:11
@VikramThakur thanks for the comment. It would be really nice if you could provide some sample code!
– kirimi
Jan 2 at 3:16
Sure. I have posted an answer you can try that.
– Vikram Thakur
Jan 2 at 3:20
What is
store.requestObject.number
in your example? Where does it come from? As is, ifcomponentDidUpdate
is called for any reason, then it'll get stuck in an infinite loop unlessstore
orrequestObject
are undefined and an error is thrown. Can you provide a minimal, verifiable example that reproduces what you're trying to do?– Drew Reese
Jan 2 at 6:52
1
Well, assuming the button click handler is calling some function to set some value in your mobx state, put the react
setState
call there as well with the same value. I'm curious now though, why do you want to duplicate app state into a component's state? Seems like a recipe for bugs. The component state should be for things ONLY the component cares about, i.e. it needs to do some internal tracking, not any whole app concerns.– Drew Reese
Jan 2 at 7:22