Skip to content
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

useForm submit should accept data #2056

Open
axelvaindal opened this issue Oct 22, 2024 · 1 comment · May be fixed by #2060
Open

useForm submit should accept data #2056

axelvaindal opened this issue Oct 22, 2024 · 1 comment · May be fixed by #2060
Labels
react Related to the react adapter

Comments

@axelvaindal
Copy link

axelvaindal commented Oct 22, 2024

Version:

  • @inertiajs/react version: latest

Describe the problem:

When using submit, put, get, post, update or delete from the useForm hook, the options parameter is typed as VisitOptions. This is because under the hood, it uses router.visit.

VisitOptions allows passing data as a parameter like this:

router.post(`/endpoint`, { data });

I think there is either a bug with the type or with the code, as it's not possible to pass data when we are using the useForm hook.

const {put} = useForm({
   foo: "bar",
});

// Below code is not working, even though the type allows it
// The data that is sent is always the one from the form, not the overwritten one
const onClick  = async () => {
   await put('/endpoint', {
      data: {
         abc: "def"
      }
   });
};

I would be in favor of fixing the code to allow passing additional data through this pattern, by merging anything that is passed in-here with the form data. This would allow injecting data at the exact moment the request is prepared to be sent, for cases where we need user confirmation (such as click) to set the data prior to sending.

I'll happily open a PR if you think it can be merged.

Steps to reproduce:

  • Install inertia in one of your project
  • Instanciate a new React component and use the useForm hook with some data
  • Try to pass additional data at the call moment, the hook always use data from the internal state/form
@axelvaindal axelvaindal added the react Related to the react adapter label Oct 22, 2024
@pedroborges
Copy link
Collaborator

pedroborges commented Oct 23, 2024

Hey @axelvaindal, you're right! useForm uses the data you initialize it with, and passing data in the visit options doesn't override it. This is the expected behavior. For your use case, you can use the transform method to modify the data before the request is sent:

const { put, transform } = useForm({
  foo: "bar",
});

const onClick = async () => {
  transform((data) => ({
    ...data,
    abc: 'def',
  }));

  await put('/endpoint');
};

Regarding the type definitions, you make a good point. We should update the type definitions to exclude the data property in this context to prevent confusion. Thank you for highlighting this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react Related to the react adapter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants