Updating a state in React looks something like below.
const [count, setCount] = useState(0)
setCount(5)
Here the new value of the count
is provided to setCount
. That updates the state
of count
to its new value.
This is the most used form of the state
update.
However, there is another form of this functionality. This looks like below.
setState( previousState=>{
// Do Something with Previous Data
// And return the newly created Data
return( newState )
} )
So the code setCount(5)
will become like below.
setCount( previousCount => { return(5) } )
This is a most useful utility once you fall into a place where you need it.
Happy Coding.