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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.