Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Reset for the LocalForm #560

Closed
justrag opened this issue Dec 7, 2016 · 5 comments
Closed

Reset for the LocalForm #560

justrag opened this issue Dec 7, 2016 · 5 comments

Comments

@justrag
Copy link

justrag commented Dec 7, 2016

Just met a scenario where I need to clear LocalForm after submit. How do I do it?

@davidkpiano
Copy link
Owner

davidkpiano commented Dec 7, 2016

Thinking about an API for this... The tricky part is that we need to have a declarative approach for this, not an imperative one. "Reaching inside" a component to do something imperatively is definitely an anti-pattern, so I don't want to attach ad-hoc instance methods or anything silly like that.

How about something like this?

<LocalForm getDispatch={(dispatch) => this.formDispatch = dispatch}>
  // ...
</LocalForm>

where getDispatch is basically like getRef - it runs once, on componentWillMount.

Then, in your parent component, you can just call:

handleReset() {
  this.formDispatch(actions.reset('local')); // or whatever model you gave to <LocalForm>
}

Does this sound like a good solution?

N.B. The reason I'm okay with this approach is, if we think about this in a functional reactive sense, a LocalForm is not just an Observable but also a PublishSubject (or just a Subject, or Producer), so we can send actions to it and also observe form state changes. (talking in terms of RxJS, if you're familiar).

@justrag
Copy link
Author

justrag commented Dec 8, 2016

I'd be happy with just
<LocalForm resetOnSubmit={true}>
:)
but your approach will help to solve many similar problems that are bound to pop up soon.

@davidkpiano
Copy link
Owner

In the short term, I can actually see resetOnSubmit being a useful property - its use-case is common enough to warrant it being a standalone prop.

@davidkpiano
Copy link
Owner

Actually, I'm going to go forward with getDispatcher because resetOnSubmit is too ambiguous/opinionated...

  • What if the initial state of the form is autofilled values and the form needs to be reset to empty values?
  • Should resetting occur at moment when submit is attempted or submit is completed?
  • What about resetting if submit fails?

@neerajhiretual
Copy link

Is there any example for resetting the localform within code (programatically) ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants