re: When should we use the appropriate way to set state? VIEW POST


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 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 πŸ˜…

code of conduct - report abuse