Comments (5)
No worries, released 0.6.0 with resetForm
.
from form-hooks.
Resetting the form is possible through one of the dependency changes. And can be triggered if the initialValues
, onSubmit
or validate
options changes like:
const { errors, touched, values } = useForm(
{ initialValues: {}, ...},
options => [options.initialValues]
);
In hindsight, any value would force a reset there so even if you wanted to have it be reset for some other state change that should be possible through something like options => [options.initialValues, otherState]
. Is there some additional case where some state change is not what should be causing the form to be reset?
from form-hooks.
Yes, that is one of the ways to reset it, but the problem here is that the only way I can change initialValues is by passing them as props and then changing them from parent component.
Not sure how to change them once I call useForm hook:
const { errors, touched, values } = useForm(
{ initialValues: { firstName: '', lastName: '' }, ...},
options => [options.initialValues]
);
I don't see how to change it after this. Only thing that comes to my mind is to create initialValues as state and then update it later to force a rerender.
One more thing is, I don't really need to change initialValues at all. So, if I have to change them just to reset the form, that feels like a hack. Also, my initialValues are going to be the same at all times, so I have to pass different initialValues (which I don't need to) just to reset form into something which is no more exactly the same (because it can't be the same as it wouldn't trigger reinitialization).
Formik has handleReset just for this use case:
https://jaredpalmer.com/formik/docs/api/formik#handlereset-void
It is usually used in cases when you submit form successfully and then you want to clear it for subsequent submits.
from form-hooks.
That makes sense, it is possible that you can do that through another state var, but I agree feels weird in those cases. I'll expose a resetForm
tonight 👍
from form-hooks.
Thanks, but don't feel rushed. I just noticed it would be nice to have this function exposed. For now the hack with handleChange works for me.
from form-hooks.
Related Issues (7)
- TypeError: (0 , _react.useState) is not a function HOT 4
- Values are not updated if you change initialValues HOT 3
- More options for `validateOnBlur` and `validateOnChange` HOT 4
- Add ability to manually set a field value HOT 2
- Ability to handle `<input type="file" />` HOT 2
- Add ability to reset a field value HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form-hooks.