I have a form setup with redux-form and basically want to create a scenario where if there's content filled in any of the form's inputs and you try to navigate away from the page you get a prompt.
The intent is to cancel the page unmount or page nav if they click Cancel. I tried creating a conditional, that if fulfilled would just return
but it still navigates away from the current page.
This is probably natural and that I'm not privy to the react/react-router workflow just yet but for the time being would anyone be able to explain the best approach for this? Is there something in general that would allow me to stop an unmount if something is unmet?
import { reduxForm } from 'redux-form';
class Form extends Component {
componentWillUnmount() {
if (!this.props.pristine && !confirm('Are you sure you want to navigate away from this page?')) {
return;
}
}
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={ handleSubmit(this.props.onSubmit) }>
...
</form>
);
}
}
...
export default connect(mapStateToProps, null)(reduxForm({
form: 'Form',
enableReinitialize: true,
validate
})(Form));