-
-
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
transform method in useForm hook doesn't work #1631
Comments
I fixed this using a ref hook. const transformRef = useRef({} as any);
useEffect(() => {
transformRef.current = (data: any) => {};
transform(transformRef.current)
}); |
v1.0.11 - the problem persists. |
This problem still persists |
Nearly 3 years and this very basic error still persists. It genuinely makes me fear for the longevity of this amazing project. Inertia is the glue that binds the frontend and backend together and is exactly how I want to build projects, I really hope this isn't a foretelling of an eventual death of the project. For anyone who needs a quick drop-in, I've built out a replacement for |
@reinink @jessarcher can we get a confirmation you are aware of this bug and that there is a PR open for it? I'm not sure why this bug has been ignored for years now. It's core functionality to one of the best features of this library. |
Surprisingly, it was working fine for me until I made an update and it stopped working, I undid all the changes and it still not working. |
Allo! Can anyone reading explain this code snippet for me? I tried it, and variations thereof, all it did was clear all data on every submit. 😕 I've been looking into this for a few hours and here's a summary of what I think I understand so far:
As much as option 3 is great, kudos to the dev!, for me, it would be most interesting to get option 1 working, a temporary patch as we faithfully await option 2 to be merged. More hints for idea 1 please? Thanks. |
Following up on using saeidalidadi's snippet. I got something working. Before: transform((data) => {
// REPLACE ME: your transform routine goes here
return data
}); After: import { useRef, useEffect } from 'react';
// ...
const transformRef = useRef({} as any);
useEffect(() => {
transformRef.current = (data: any) => {
// REPLACE ME: your transform routine goes here
return data
};
transform(transformRef.current)
}, [data]); Then, add |
Same here. First it is working perfectly fine until I make some modification to the code. But undo changes is not helping either. One thing I noticed that it is working after the code has an error and fix it. But It is not working after refresh. |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
I've been looking into this one and believe I arrived at the crux of the confusion. The This works: const Page = () => {
const { post, transform } = useForm({});
transform((data) => ({ ...data, foo: "bar" }));
const handleSubmit = (e) => {
e.preventDefault();
post("/route");
}
// ...
} This does not work: const Page = () => {
const { post, transform } = useForm({});
const handleSubmit = (e) => {
e.preventDefault();
transform((data) => ({ ...data, foo: "bar" }));
// A re-render gets triggered somewhere in `post`,
// which wipes out the `transform` call just made
post("/route");
}
// ...
} (Discussed here: #1491 (comment)) |
Then, as pointed in #1491, I think it should be noted in the docs that |
Thanks @andcl. Could you maybe attempt a PR to the docs? |
Even if the originally intended behavior of the transform method is to be used in the component's body, It will be much more useful if could be used like #1131. At least for me it feels hacky storing a value in a ref or somewhere else to be later used in the transform. Maybe a solution like the one proposed here could also work. Another posibility could be to merge the form state with the data provided in the submit method. I would make this have precedence over the current state so it can be overriden. Currently the data is ignored and only the form state is submitted.
|
This has been fixed by #1607. |
Version:
@inertiajs/react
version: 1.0.9Describe the problem:
I'm re-reporting an issue which was closed in the recent reset of issues, the original is #1131
When using
useForm
from the React package, thetransform
method doesn't work. The method is defined in the body of theuseForm
component, and thus is re-initialized upon re-renders to be the original "placeholder" method.The transform method needs to be memoized in some way. I submitted #1491 which I've tested and it addresses this issue. It stores the
transform
method in aref
so that when it's value is updated it persists through re-renders.The text was updated successfully, but these errors were encountered: