I don't really like the syntax used in Manage multiple states, but I guess it works.
One case to consider is that of batching, and how it is not consistent up to React 17.
This handler for example:
const handler = () => { fetch("https://pokeapi.co/api/v2/pokemon/pikachu").then(() => { setData(() => { return { name: "Joe" }; }); setLoading(() => { return "Done"; }); }); };
Triggers two, albeit very quick, renders. React 18 will fix this, but in the mean time, useReducer does the trick.
useReducer
Checkout this CodeSandBox, notice that the useState trigger logs two render counts per click, while the useReducer example logs one render count.
useState
Very interesting... Thanks!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
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.
I don't really like the syntax used in Manage multiple states, but I guess it works.
One case to consider is that of batching, and how it is not consistent up to React 17.
This handler for example:
Triggers two, albeit very quick, renders. React 18 will fix this, but in the mean time,
useReducer
does the trick.Checkout this CodeSandBox, notice that the
useState
trigger logs two render counts per click, while theuseReducer
example logs one render count.Very interesting... Thanks!