In this episode, we're tackling a common issue you'll notice when working with forms: after submitting, the text you've typed sticks around rather than clearing itself out. This happens because we're not doing a full page reload; we're just re-rendering the component. So, your form's local state doesn't automatically reset.
To fix this, we explore the different ways you can reset your form fields once the form is submitted. First, we look at the straightforward approach you might use—just manually setting your form data (like form.body = ''
). While this clears the input, it's a bit of a pain if your form has more than one field or if you have default values you want to keep.
That's where Inertia's reset
helper comes in! Instead of manually resetting everything, you can simply call reset()
on your form, and it will restore all form fields to their initial state. Super handy, especially if you have forms with some pre-filled data (like updating a user's profile). We check out how this works in practice using Laravel Breeze's profile update form as an example, showing how the reset method keeps default values intact instead of just blanking everything out.
So by the end of the episode, you'll see how using Inertia's methods not only keeps your forms tidier but also makes handling defaults and form resets much easier.