-
Notifications
You must be signed in to change notification settings - Fork 472
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
[React] Use updater function in setData
in useForm
hook
#1859
Conversation
Update: I just noticed that the current behaviour is very unexpected because it overrides previous changes not only from key-value calls but all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one looks safe & good to me!
useForm
hook
useForm
hooksetData
in useForm
hook
@rrmesquita Hey thanks for catching this! 🙏 And thanks for the review @derrickreimer and @thecrypticace 🤙 |
Just upgraded to 1.2.0 with hope that it'll be fixed but it seems to still be bugged. |
This patch is not released yet, but will go out in the next version. |
Is there a smooth workaround for this? |
You can use a updater functions instead for now: setData(prev => ({
...prev,
name: 'Bob',
lastname: 'Smith'
})) |
You can extend the hook locally: // file: hooks/useForm.ts
import { useForm as useInertiaForm } from '@inertiajs/react'
export default function useForm<T extends Record<string, unknown>>(initialValues = {} as T) {
const form = useInertiaForm(initialValues)
return {
...form,
setData(keyOrData: keyof T | ((data: T) => T) | T, value?: T[keyof T]) {
if (typeof keyOrData === 'string') {
form.setData((data) => ({ ...data, [keyOrData]: value }))
} else if (typeof keyOrData === 'function') {
form.setData((data) => keyOrData(data))
} else {
form.setData(keyOrData)
}
},
// Other useful helpers
}
} |
Thanks for this. But I didn't want to extend in order not to cause a conflict with the patch when it is released. So what I have done is:
|
It doesn't seem to me that the problem is solved in 1.3.0, am I wrong? |
@matteogilioli it should be! Might sharing some relevant code where you're seeing something not behaving as expected? |
Sorry, my fault. Now it seems to have resolved, I have no idea what changed in the meantime, maybe some caching problem. |
I can confirm this is fixed in 1.3.0-beta.2, I just tested it. |
hi, how can i use that? just import it from a hook file instead of inertia ? |
@vitalijalbu yes, like so: import useForm from '../resources/lib/hooks' |
I tried to upgrade to this version but cannot make it happen:
|
@vitali-bc that is a different problem. |
I dont' have a form but only a onClick button, in this situation how can i update form data? |
This is a proposal for changing the ergonomics of the key-value variant for the
setData
hook in React.Current behaviour
Using this simple form as an example:
It is expected that sequential calls to
setData
will update the form data accordingly. However, subsequent calls overwrite the changes made by the previous calls.Desired behaviour
Sequential
setData
calls use an updater function to batch the changes from multiple calls.This can be achieved using updater functions for the set functions in React. From React documentation:
It is worth noting that this only affects the key-value variant of the
setData
function, and if anyone is impacted by this change, they can switch to the object variant of thesetData
function.