angular-redux / form

Keep your Angular2+ form state in Redux
MIT License
41 stars 15 forks source link

How can I reset the form state? #56

Closed yuriyward closed 6 years ago

yuriyward commented 6 years ago

This is a...

What toolchain are you using for transpilation/bundling?

Environment

NodeJS Version: 8.10 Typescript Version: ~2.5.3 Angular Version: 5 @angular-redux/store version: 7 @angular-redux/form version: 6.7 @angular/cli version: ~1.7.3 OS: Windows

Expected Behaviour:

How can I reset the form state?

I'm just added this <form connect="name"> and <input ngModel ngControl ...>

And I want in component.ts after some action reset state of the form "name"?

Actual Behaviour:

-

Stack Trace/Error Message:

-

maplion commented 6 years ago

Hi @victorward, You should be able to accomplish this by dispatching your other action and then returning a state with name as an empty string.

For example: in your reducer that is listening to your action that is supposed to reset your form name, return the state object with the name value set to an empty string:

...
case 'MY_ACTION':
  return { ...state, name: '' }
...

I recommend you have your component call a service that does the dispatching of actions.

yuriyward commented 6 years ago

Hi @maplion, thank you for your response. I know that I can create my action creator and dispatch this action by myself, but I thought that there is some automatic way. In any case thanks for the reply.

maplion commented 6 years ago

@victorward Gotcha. Yeah, no automatic way that I know. If you were so inclined, I imagine it wouldn't be too difficult to make a fork and then add your own code that takes a default action call with your form path as a payload that then does the reset for you on the given form (a generic packaged version of the above).