Git Product home page Git Product logo

Comments (5)

BenMagyar avatar BenMagyar commented on July 20, 2024 1

No worries, released 0.6.0 with resetForm.

from form-hooks.

BenMagyar avatar BenMagyar commented on July 20, 2024

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.

niksajanjic avatar niksajanjic commented on July 20, 2024

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.

BenMagyar avatar BenMagyar commented on July 20, 2024

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.

niksajanjic avatar niksajanjic commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.