unstable_usePrompt
unstable_usePrompt フックを使用すると、現在の場所から移動する前に、window.confirm を介してユーザーに確認を求めることができます。
function ImportantForm() {
const [value, setValue] = React.useState("");
// 入力にデータが入力された場合、他の場所に移動するのをブロックします
unstable_usePrompt({
message: "よろしいですか?",
when: ({ currentLocation, nextLocation }) =>
value !== "" &&
currentLocation.pathname !== nextLocation.pathname,
});
return (
<Form method="post">
<label>
重要なデータを入力してください:
<input
name="data"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</label>
<button type="submit">保存</button>
</Form>
);
}