I suggest you memoize the updateValue API because now it's being recreated on every rerender (state change).
You can use a ref for that, let's say:
const[value,setValue]=useState(initialValue??false);constupdateApiRef=useRef(null);// null just for initialization.if(updateApiRef.current===null){// This will initialize the API with a memoized object.updateApiRef.current={toggle:()=>setValue(oldValue=>!oldValue),on:()=>setValue(true),off:()=>setValue(false),}}// At this point, updateApiRef.current is not equal to null anymore, before even returning it.return[value,updateApiRef.current];
I hope this makes sense, writing code on a phone is rly hard π₯΅
I suggest you memoize the updateValue API because now it's being recreated on every rerender (state change).
You can use a ref for that, let's say:
I hope this makes sense, writing code on a phone is rly hard π₯΅
Or you just could use useMemo() for this, much easier:
You don't need setValue as a dep (because it's part of useState). I'd probably say useRef is better but you'd do it similarly to how you have.
That being said I generally just use useState without any Boolean hook.
You're right, I think useRef makes sense in this case and the way you used it. Thanks for pointing that out.