You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usest8 is a single function alternative for the useState hook (typically: const [currentValue, updater] = useState(initial)), that combines the current value constant and updater function into a single function.
// create a new local state value in a React function component constcount=useSt8(0)
// same, but with initializer function constcount=useSt8(()=>0)
// get the current state count()
// change the state with to the given value count(0)
// update the state using an updater function, that receives the current state and returns the next one count(c=>c+1)
useSt8 has the same call-signature as the React useState hook.
Except, instead of returning a tuple with the current value and a setter, it returns a single function.
The function returned can be called in two different ways:
With zero arguments. In that case the current state is returned.
With one argument. In that case the current state is updated with the given value, or using an updater function, just like the normal useStateupdate function.
That's all.
Benefits
No array destructurings needed, which polute your closures with name pairs, like const [count, setCount] = useState(0). Instead, const count = useSt8(0) just reads nicer. And it saves some characters. Super important. All IMHO .
Doesn't rely on array destructurings, which are potentially slow as they use the iterator protocol (background). Note that you probably won't notice this in practice, so this is more of a fun fact than an argument to use this.