3 ms·
I don't really understand extendState because it seems like somewhat pointless syntactic sugar for: setState((previousState) => ({...previousState, foo: false}
by fastball 5y ago
I don't really understand extendState because it seems like somewhat pointless syntactic sugar for:
setState((previousState) => ({...previousState, foo: false}))
- ui4jd73bdj 5y agoI would imagine it would help more with nested objects. ({ contact: { address: "" } }) as opposed to ({ ...prev, contact: { ...prev.contact, address: "" } })
- zeroonetwothree 5y agoI guess how you should handle that is ambiguous. Probably best to be explicit for that reason.
- loh 5y agoFor deeply nested data, I would probably use `immutability-helper`. Many deep spread operators can get ugly and confusing pretty quickly, in my experience.
- loh 5y agoYep! It is syntactic sugar. I personally like it though because it results in slightly cleaner, more concise code. It's just easier on my eyes.
- fastball 5y agoNormally syntactic sugar is fine and has a near-zero cost, but in this case I don't think that's actually true. Given the way hooks are structured, if I want to use both `setState` and `extendState` in a `useCallback`, I'd need to pass both to the dependency array, which is just extra effort (which defeats the purpose of the syntactic sugar imo).