DEV Community

loading...

Discussion on: My 5 cents about React Hooks

Collapse
guilhermetoti profile image
Guilherme Toti Author

When you use call the setter with a function as parameter, like you did, the “prev” (the function argument) is the current state.
In your example, the “prev” is the current value of “data”.
You usually do that way instead of just setData(something) when you probably have state changes with a very short period and you want to make sure you are using the latest state value possible.
As setState is async, you may call it before other state change be completed and use old values. Using it with a parameter grants you using the latest state.

Collapse
drewkiimon profile image
Andrew Pagan

That makes sense thank you! :)