DEV Community

Discussion on: React hooks & the closure hell 2

Collapse
 
anpos231 profile image
anpos231

I see.
My solution was is definitely not the best one here.

I'd like to encourage you to check @shiatsumat answer above. It's probably the cleanest way to approach this problem, this method was even mentioned in the official react docs!

The problem with your solution is that it's not clear what valueRef.current is and how to handle it. Some developers might try setting valueRef.current directly, and be surprised that it does not work.

@shiatsumat solution allows you to write the code like this, but without valueRef. Just a single custom hook.