[Bug]: Setting value on a captured reference to the set function on useControlField
doesn't update the value
#319
Labels
bug
Something isn't working
Which packages are impacted?
remix-validated-form
@remix-validated-form/with-zod
@remix-validated-form/with-yup
zod-form-data
What version of these packages are you using?
latest
Please provide a link to a minimal reproduction of the issue.
https://codesandbox.io/p/sandbox/ecstatic-haze-y4wtdf?file=%2Fapp%2Froutes%2Findex.tsx%3A26%2C21
Steps to Reproduce the Bug or Issue
In the forked starter template, the first field uses
useControlField
and works as expected. The second field captures thesetValue
function with auseCallback
and entering text into the field no longer works. The same problem happens when we useuseMemo
or any other function which captures a reference to the setter function. The same issue also seems to happen withuseUpdateControlField
as well.This can cause really difficult to debug issues, since it is very easy with
useEffect
,useCallback
and so on to accidentally use an "outdated" version of the setter function. It'd be really great if using an old version of it worked just fine.In the starter template we also illustrated an ugly workaround using
useRef
. By using a ref and always setting it to the latest version of the setter function everything works as expected.Expected behavior
I would want the setter function to work regardless of whether it has been captured in a previous render pass.
Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: