image

Another trick is that the setter function from useState can accept a function. This can also help save unnecessary rerenders.

const [open, setOpen] = useState(false)
const toggleState = useCallback(() => setOpen(!open), [open])
const toggleStateCool = useCallback(() => setOpen(open => !open), [])

Why? if another hook depends on toggleState callback, it won't require another rerender. Probably not useful if used in simple cases like toggling a state, but may be useful if you are making your own custom hooks with more complexity.