Skip to content

Commit

Permalink
docs: tips for form reset with default value from loader (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung authored Jun 4, 2024
1 parent 85f0eed commit f60e4bf
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/integration/remix.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,28 @@ export default function Login() {
);
}
```

## Tips

### The default value might be out of sync if you reset the form from the action

If the default value of the form comes from the loader and you are trying to reset the form on the action, there is a chance you will see the form reset to the previous default value. This is Conform will reset the form the moment actionData is updated while Remix is still revalidating the loader data. To fix this, you can wait for the state to be `idle` (e.g. `navigation.state` or `fetcher.state`) before passing the `lastResult` to Conform like this:

```tsx
export default function Example() {
const { defaultValue } = useLoaderData<typeof loader>();
const lastResult = useActionData<typeof action>();
const navigation = useNavigation();
const [form, fields] = useForm({
// If the default value comes from loader
defaultValue,

// Sync the result of last submission only when the state is idle
lastResult: navigation.state === 'idle' ? lastResult : null,

// ...
});

// ...
}
```

0 comments on commit f60e4bf

Please sign in to comment.