I create registration/login/forgot-password forms using redux-form. Also I use react-intl for multilingual support. Changing language causes rerender of components and resets the forms.
I would like to reset form only when location is changed, but my solution seems to me not perfect
1) if I use destroyOnUnmount: true
the case is next: when user started to fill e.g. registration form and then decided to change language -> form destroys on unmount and all fields are reset.
2) if destroyOnUnmount: false
everything is OK, on language change form data doesn't destroy. But if to change route and then open again registration form - previous data is still present.
initialValues are not set
So I decided to set destroyOnUnmount: false
Then in App component I call action if location changes
// reset redux forms on route change
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
this.props.resetFormsAction();
}
}
and then in saga listen to action
export function* resetForms() {
try {
const forms = yield select(getForms);
yield all(Object.keys(forms).map(form => put(reset(form))));
} catch (e) {
console.error(e);
}
}
const commonSagas = [
takeEvery(RESET_FORMS, resetForms),
];
export default commonSagas;
selector
export const getForms = (state: AppStore) => state.form;
This solution works fine, but is there any other implementation to reset/destroy form only when location is changed but not when change language is clicked
I have found similar question but it didn't help me