Wrapper around useBlocker to show a window.confirm prompt to users instead of building a custom UI with useBlocker.
The unstable_ flag will not be removed because this technique has a lot of rough edges and behaves very differently (and incorrectly sometimes) across browsers if users click addition back/forward navigations while the confirmation is open. Use at your own risk.
functionImportantForm() { let [value, setValue] = React.useState("");
// Block navigating elsewhere when data has been entered into the input unstable_usePrompt({ message:"Are you sure?", when: ({ currentLocation, nextLocation }) => value !== "" && currentLocation.pathname !== nextLocation.pathname, });
return ( <Formmethod="post"> <label> Enter some important data: <input name="data" value={value} onChange={(e) =>setValue(e.target.value)} /> </label> <buttontype="submit">Save</button> </Form> ); }
Wrapper around useBlocker to show a window.confirm prompt to users instead of building a custom UI with useBlocker.
The
unstable_
flag will not be removed because this technique has a lot of rough edges and behaves very differently (and incorrectly sometimes) across browsers if users click addition back/forward navigations while the confirmation is open. Use at your own risk.