I'm often in a situation, where I like to have a reference (with useRef) in sync with a state (with useState). This is particularly useful inside stable callbacks to avoid unnecessary rerenders.
Currently I'm using this approach:
const [value, setValue] = useState(...)
const valueRef = useRef(value)
valueRef.current = value
I've seen code where the last line is wrapped in a useEffect like
useEffect(() => {
valueRef.current = value
}, [value])
which looks more expensive as simply overwriting the previous value with the new one.
Is there a best practice for something like that? Or is the complete concept wrong?
Thanks in advance