Open MarkHesk opened 2 years ago
Maybe not the most efficient way, but you could save the values you want to keep before resetting and then set them back.
I certainly think there's a better way to write this than setting each control individually. You can set the form value as a whole, in a single action. To what @Balastrong said, just grab the value you don't want to reset, and make sure that is the value you set on top of your initial state. My approach would be to do this in your own action, something like
// dispatch action you've created - obviously name whatever makes sense to you
this.store.dispatch(softResetForm())
/* in your reducer */
on(actions.softResetForm, (state) => {
// get the current value for `stage`
const { stage } = state.formState.value;
// create a value from your initial form state, with `stage` overridden with the value above
const softResetValue = { ...DOCTRANS_FORM_INITIAL_STATE.value, stage };
// use `reset()` on your formState if, if you want reset to pristine - if not leave out
// use `setValue()` on the entire formState, providing the value defined above
const formState = setValue(reset(state.formState), softResetValue);
// update your state
return { ...state, formState };
})
Hey, hope this is the right place to post it.
I have the following reducer.ts:
When i want to reset the form i can call:
But i would like to reset it except of the stage.
What i do now:
But i think there must be a better way. Maybe some reducer which says "reset all but keep stage".
Thanks