Software Engineering Manager and Endurance Running Race Director - Have lots of ideas of how to simplify and automate endurance race management through workflow optimization and software development.
Perhaps my question wasn't clear enough, sorry for that. I posted the two options (from the React docs) knowing that those were the correct ways to implement state, BUT my question is when to use one over the other. Why are there two correct ways when Option 2, IMO, should always be used. Why have Option 1, when there's Option 2? Hope that clears it up.
How I understood was that they are basically the same providing devs multiple ways to do the same thing depending on your need.
So if you don't need to use previous state or props, then go with option 1 else option 2.
Unrelated to the main question,
For memoization in React Hooks, they have two different ways to memoize (useCallback & useMemo, which do the same thing but useCallback is called like Option 1 of setState, while useMemo is called like Option 2 of setState).
Official doc shows that
useCallback(fn, inputs) is equivalent to useMemo(() => fn, inputs).
But I don't know enough history about React why there is only setState but two for memoization 😅
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Sung,
Thanks for your reply!
Perhaps my question wasn't clear enough, sorry for that. I posted the two options (from the React docs) knowing that those were the correct ways to implement state, BUT my question is when to use one over the other. Why are there two correct ways when Option 2, IMO, should always be used. Why have Option 1, when there's Option 2? Hope that clears it up.
Yikes.. I am sorry I misunderstood the question 😰
How I understood was that they are basically the same providing devs multiple ways to do the same thing depending on your need.
So if you don't need to use previous state or props, then go with option 1 else option 2.
Unrelated to the main question,
For memoization in React Hooks, they have two different ways to memoize (useCallback & useMemo, which do the same thing but
useCallback
is called like Option 1 ofsetState
, whileuseMemo
is called like Option 2 ofsetState
).Official doc shows that
But I don't know enough history about React why there is only
setState
but two for memoization 😅