You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 oneconstonClick=async()=>{awaitput('/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
The text was updated successfully, but these errors were encountered:
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:
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!
Version:
@inertiajs/react
version: latestDescribe the problem:
When using
submit
,put
,get
,post
,update
ordelete
from theuseForm
hook, the options parameter is typed asVisitOptions
. This is because under the hood, it usesrouter.visit
.VisitOptions
allows passingdata
as a parameter like this: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 theuseForm
hook.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:
useForm
hook with some dataThe text was updated successfully, but these errors were encountered: