Skip to content
TwitterGithub

React Query and Forms

ReactJs, React Query, TypeScript, JavaScript5 min read

forms
Photo by Kelly Sikkema


  • 한국어
  • Add translation

Disclaimer: Please note that I will be using react-hook-form throughout this article, merely because I think it's a great library. That doesn't mean that the shown patterns will only work with react-hook-form - the concepts are applicable to any form library, and also if you are not using a form library at all.


Forms are an important part in many web applications as the primary means to update data. We are using React Query not only to fetch data (queries), but also to modify it (mutations), so we need to somehow integrate our beloved async state manager with forms.

The good news is that realistically, there isn't anything special about forms: It is still just a bunch of html elements that we render in order to display some data. However, as we'd also like to change that data, the lines between what is Server State and what is Client State start to blur a bit, which is where the complexity might come in.

Server State vs. Client State

To recap, Server State is state that we do not own, that is mostly async and where we only see a snapshot of how the data looked like the last time we fetched it.

Client State is state that the frontend has full control over, is mostly synchronous and where we know the accurate value of it at all times.

When we display a list of Persons, that is undoubtedly Server State. But what happens when we click on a Person to show their details in a Form with the intention of maybe updating some values? Does that Server State now become Client State? Is it a hybrid?

The simple approach

I have gone on the record already about how I am not a fan of copying state from one state manager to another, be it putting props to state or copying state from React Query to local state.

I do think that forms can be an exception to this rule though, if you are doing it deliberately and know about the tradeoffs (everything is a tradeoff after all). When rendering our Person form, we very likely want to treat the Server State as initial data only. We fetch the firstName and lastName, put it into the form state, and then let the user update it.

Let's take a look at an example:

simple-form
1function PersonDetail({ id }) {
2 const { data } = useQuery(['person', id], () => fetchPerson(id))
3 const { register, handleSubmit } = useForm()
4 const { mutate } = useMutation((values) => updatePerson(values))
5
6 if (data) {
7 return (
8 <form onSubmit={handleSubmit(mutate)}>
9 <div>
10 <label htmlFor="firstName">First Name</label>
11 <input {...register('firstName')} defaultValue={data.firstName} />
12 </div>
13 <div>
14 <label htmlFor="lastName">Last Name</label>
15 <input {...register('lastName')} defaultValue={data.lastName} />
16 </div>
17 <input type="submit" />
18 </form>
19 )
20 }
21
22 return 'loading...'
23}

This works incredibly well - so what are those tradeoffs?

Data might be undefined

You might know that useForm would also take defaultValues directly for the whole form, which would be pretty nice for larger forms. However, because we cannot call hooks conditionally, and because our data is undefined on the first render cycle (as we need to fetch it first), we cannot just do this in the same component:

no-default-values
1const { data } = useQuery(['person', id], () => fetchPerson(id))
2// 🚨 this will initialize our form with undefined
3const { register, handleSubmit } = useForm({ defaultValues: data })

We'd have the same problem when copying into useState, or when using uncontrolled forms (which react-hook-form does under the hood by the way). The best solution to this would be to split up the form into its own component:

separate-form
1function PersonDetail({ id }) {
2 const { data } = useQuery(['person', id], () => fetchPerson(id))
3 const { mutate } = useMutation((values) => updatePerson(values))
4
5 if (data) {
6 return <PersonForm person={data} onSubmit={mutate} />
7 }
8
9 return 'loading...'
10}
11
12function PersonForm({ person, onSubmit }) {
13 const { register, handleSubmit } = useForm({ defaultValues: person })
14 return (
15 <form onSubmit={handleSubmit(onSubmit)}>
16 <div>
17 <label htmlFor="firstName">First Name</label>
18 <input {...register('firstName')} />
19 </div>
20 <div>
21 <label htmlFor="lastName">Last Name</label>
22 <input {...register('lastName')} />
23 </div>
24 <input type="submit" />
25 </form>
26 )
27}

This is not too bad, as it separates our data fetching from the presentation. I'm personally not a big fan of such a split, but it does get the job done here.

No background updates

React Query is all about keeping your UI up-to-date with Server State. As soon as we copy that state somewhere else, React Query cannot do its job anymore. if a background refetch happens for whatever reason, and it yields new data, our form state will not update with it. This is likely not problematic if we are the only one working on that form state (like a form for our profile page). If that's the case, we should likely at least disable background updates by setting a higher staleTime on our query. After all, why would we keep querying our server if the updates will not be reflected on the screen?

no-background-updates
1// ✅ opt out of background updates
2const { data } = useQuery(['person', id], () => fetchPerson(id), {
3 staleTime: Infinity,
4})

This approach can get problematic on bigger forms and in collaborative environments. The bigger the form, the longer it takes our users to fill it out. If multiple people work on the same form, but on different fields, whoever updates last might override the values that others have changed, because they still see a partially outdated version on their screen.

Now react hook form allows you to detect which fields have been changed by the user and only send "dirty" fields to the server with some user land code (see the example here), which is pretty cool. However, this still doesn't show the latest values with updates made by other users to you. Maybe you would change your input had you known that a certain field was changed in the meantime by someone else.

So what would we need to do to still reflect background updates while we are editing our form?

Keeping background updates on

One approach is to rigorously separate the states. We'll keep the Server State in React Query, and only track the changes the user has made with our Client State. The source of truth that we display then to our users is derived state from those two: If the user has changed a field, we show the Client State. If not, we fall back to the Server State:

separate-states
1function PersonDetail({ id }) {
2 const { data } = useQuery(['person', id], () => fetchPerson(id))
3 const { control, handleSubmit } = useForm()
4 const { mutate } = useMutation((values) => updatePerson(values))
5
6 if (data) {
7 return (
8 <form onSubmit={handleSubmit(mutate)}>
9 <div>
10 <label htmlFor="firstName">First Name</label>
11 <Controller
12 name="firstName"
13 control={control}
14 render={({ field }) => (
15 // ✅ derive state from field value (client state)
16 // and data (server state)
17 <input {...field} value={field.value ?? data.firstName} />
18 )}
19 />
20 </div>
21 <div>
22 <label htmlFor="lastName">Last Name</label>
23 <Controller
24 name="lastName"
25 control={control}
26 render={({ field }) => (
27 <input {...field} value={field.value ?? data.lastName} />
28 )}
29 />
30 </div>
31 <input type="submit" />
32 </form>
33 )
34 }
35
36 return 'loading...'
37}

With that approach, we can keep background updates on, because it will still be relevant for untouched fields. We are no longer bound to the initialState that we had when we first rendered the form. As always, there are caveats here as well:

You need controlled fields

As far as I'm aware, there is no good way to achieve this with uncontrolled fields, which is why I've resorted to using controlled fields in the above example. Please let me know if I'm missing something.

Deriving state might be difficult

This approach works best for shallow forms, where you can easily fall back to the Server State using nullish coalesce, but it could be more difficult to merge properly with nested objects. It might also sometimes be a questionable user experience to just change form values in the background. A better idea might be to just highlight values that are out of sync with the Server State and let the user decide what to do.


Whichever way you choose, try to be aware of the advantages / disadvantages that each approach brings.

Tips and Tricks

Apart from those two principal ways of setting up your form, here are some smaller, but nonetheless important tricks to integrate React Query with forms:

Double submit prevention

To prevent a form from being submitted twice, you can use the isLoading prop returned from useMutation, as it will be true for as long as our mutation is running. To disable the form itself, all you need to do is to disable the primary submit button:

disabled-submit
1const { mutate, isLoading } = useMutation((values) => updatePerson(values))
2<input type="submit" disabled={isLoading} />

Invalidate and reset after mutation

If you do not redirect to a different page right after the form submission, it might be a good idea to reset the form after the invalidation has completed. As described in Mastering Mutations, you'd likely want to do that in the onSuccess callback of mutate. This also works best if you keep state seperated, as you only need to reset to undefined in order for the server state to be picked up again:

reset-form
1function PersonDetail({ id }) {
2 const queryClient = useQueryClient()
3 const { data } = useQuery(['person', id], () => fetchPerson(id))
4 const { control, handleSubmit, reset } = useForm()
5 const { mutate } = useMutation(updatePerson, {
6 // ✅ return Promise from invalidation
7 // so that it will be awaited
8 onSuccess: () => queryClient.invalidateQueries(['person', id]),
9 })
10
11 if (data) {
12 return (
13 <form
14 onSubmit={handleSubmit((values) =>
15 // ✅ rest client state back to undefined
16 mutate(values, { onSuccess: () => reset() })
17 )}
18 >
19 <div>
20 <label htmlFor="firstName">First Name</label>
21 <Controller
22 name="firstName"
23 control={control}
24 render={({ field }) => (
25 <input {...field} value={field.value ?? data.firstName} />
26 )}
27 />
28 </div>
29 <input type="submit" />
30 </form>
31 )
32 }
33
34 return 'loading...'
35}

That's it for today. Feel free to reach out to me on twitter if you have any questions, or just leave a comment below. ⬇️