unstable_usePromptdeclare function unstable_usePrompt({ when, message, }: { when: boolean | BlockerFunction; message: string; }) { type BlockerFunction = (args: { currentLocation: Location; nextLocation: Location; historyAction: HistoryAction; }) => boolean; interface Location<State = any> extends Path { state: State; key: string; } interface Path { pathname: string; search: string; hash: string; } enum HistoryAction { Pop = "POP", Push = "PUSH", Replace = "REPLACE", } The unstable_usePrompt hook allows you to prompt the user for confirmation via window.confirm prior to navigating away from the current location.
function ImportantForm() { 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 ( <Form method="post"> <label> Enter some important data: <input name="data" value={value} onChange={(e) => setValue(e.target.value)} /> </label> <button type="submit">Save</button> </Form> ); }