-
-
Notifications
You must be signed in to change notification settings - Fork 446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memoize transform
to fix long standing useForm
bug
#1718
Conversation
Merge main project changes
Merge updates from main project
@reinink Any idea when this might be merged? 🙏 |
I need this fix as well! The Repro steps: const myCallback = (data) => {
console.log("myCallback")
return data
}
form.transform(myCallback)
form.post('some/url', { onSuccess: ({ props }) => {
form.setData(props.formData)
form.post('some/url') // "myCallback" is not printed out
}) |
I added a package containing this fix in the npm registry if anyone is interested: |
Yay, thanks! Now what? |
Please forgive me for tagging... @claudiodekker @jessarcher @thecrypticace |
It would sure be great to get this long standing bug fixed. Hoping this is the bump that gets attention. |
I just tried to reproduce the issue described here but couldn't 🤷♂️ Am I missing something? I tested with the code below on the React Playground's const form = useForm('NewUser', {
name: '',
company: '',
role: '',
})
function submit(e) {
e.preventDefault()
form.transform((data) => ({ ...data, name: 'Xyz' }))
form.post('/user')
} |
By the way, it seems #1607 fixes this and some other issues. |
Hey @pedroborges, I've done some work on this one. Fundamentally there's a race condition at play here: if a rerender triggers before the custom transform function gets called, then the custom transform will get overwritten. (More explanation here: #1491 (comment)). This is because // This will get reset every time the component renders
let transform = (data) => data
// ...
return {
transform(callback) {
transform = callback
}, My recommendation is that we merge #1607 (with thanks to @aviemet for the work here as well), since it includes this fix and some additional memoization to mitigate unnecessary re-renders. |
Thanks @derrickreimer, I'll take your advice and close this on in favor of #1607. Thanks for your work and patience @aviemet, I'll let you know when the other PR is merged. |
I'm opening another PR to fix this bug (#1171, #1131, #1491, #1631)
In the React
useForm
package, thetransform
function is set as a local variable in the component. This means that if the page triggers any re-render, thetransform
variable is re-initialized to the placeholder function ((data) => data
). For this reason, it's more likely than not that the transform method does nothing, rendering the docs inaccurate.The previous PRs were closed without being read. I think the whole React community here would really appreciate this bug finally being fixed.