Open utterances-bot opened 2 years ago
I don't understand what's going on here?
const handleClick = () => {
setCount((prev) => {
setPrevCount(prev);
setCount(count + 1);
});
};
handleClick()
is an event handler functionsetCount()
, which creates a function with the argument prev
prev
get passed in? Is it the implicit event
from handleClick()
?setPrevCount()
sets the prevCount
to prev
...except I still don't know what prev
issetCount()
runs, that sets count
to count + 1
setCount()
(that got passed a function) sets count
to...I don't know what?@Ever-It-Lazy the prev
is implicit parameter in setCount()
which keeps track of previous value. Every setFunction in useState hook has it as a parameter. In this context we want to update prevCount
variable to current count, as we have to update the current count
to +1.
State
Working with the useState hook
https://fireship.io/courses/react/basics-state/