The documentation says that the callback always works, but I know from experience that it doesn't always return what you're expecting. I think it has something to do with using mutable objects inside the state itself.
Docs: https://reactjs.org/docs/react-component.html#setstate
You can't completely rely on the callback. Instead, what you can do is create
var stateObject = this.state
Make any necessary changes to the object:
stateObject.monthOffset -= 1
and then set the state like this:
this.setState(stateObject);
That way you have a copy of the nextState
inside stateObject
To clarify: You want to do all of the evaluation before you set the state, so do:
monthOffset -= 1
then if (monthOffset === 12) yearOffset -=1;
then var stateObj = {monthOffset: monthOffset, yearOffset: yearOffset}
then this.setState(stateObj);
From the documentation: "The second parameter to setState()
is an optional callback function that will be executed once setState is completed and the component is re-rendered. Generally we recommend using componentDidUpdate() for such logic instead."
So basically if you want to get the next state, you should either have a copy of it inside your function that calls setState()
or you should get the nextState
from componentDidUpdate
Afterthoughts: Anything that you pass into setState()
as a parameter is passed by reference (not by value). So, if you have an object SearchFilters: {}
within your state, and inside your call to setState()
, you have
setState({SearchFilters: DEFAULT_SEARCH_FILTERS}); // do not do this
You may have set SearchFilters
to DEFAULT_SEARCH_FILTERS
in an effort to clear out a form called "Search Filters", but instead you will have effectively set DEFAULT_SEARCH_FILTERS
(a constant) to SearchFilters
, clearing out your DEFAULT_SEARCH_FILTERS
.
Expected behavior? You tell me.
console.log("yearOffset", yearOffset)
instead of an anonymous function inside the setState callback? – Sundried