inertia icon indicating copy to clipboard operation
inertia copied to clipboard

Memoize `transform` to fix long standing `useForm` bug

Open aviemet opened this issue 1 year ago • 6 comments

I'm opening another PR to fix this bug (#1171, #1131, #1491, #1631)

In the React useForm package, the transform function is set as a local variable in the component. This means that if the page triggers any re-render, the transform 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.

aviemet avatar Nov 06 '23 20:11 aviemet

@reinink Any idea when this might be merged? 🙏

mattsims avatar Dec 13 '23 15:12 mattsims

I need this fix as well! The transform callback is being overridden by the default (data) => data after doing something basic like form.setData.

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
})

mochetts avatar Dec 19 '23 13:12 mochetts

I added a package containing this fix in the npm registry if anyone is interested: https://www.npmjs.com/package/@moraki/inertia-react

mochetts avatar Dec 19 '23 14:12 mochetts

Yay, thanks! Now what?

dac514 avatar Dec 28 '23 15:12 dac514

Please forgive me for tagging... @claudiodekker @jessarcher @thecrypticace Can this PR get some feedback? Thanks for your time and hard work.

dac-humi avatar Feb 21 '24 21:02 dac-humi

It would sure be great to get this long standing bug fixed. Hoping this is the bump that gets attention. @reinink

aviemet avatar Mar 12 '24 02:03 aviemet