Skip to content
Snippets Groups Projects

useBlock in the survey frontend to ask users for confirmation

Merged Remco Tukker requested to merge feature/react-router-stuff into develop
2 files
+ 38
4
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 30
0
import React from "react";
import { unstable_useBlocker as useBlocker } from "react-router-dom";
// adapted from https://stackoverflow.com/a/75920683
// also see https://github.com/remix-run/react-router/blob/496b1fe8253643171ecca6e6a945d98386c4eb00/packages/react-router-dom/index.tsx#L1460C4-L1460C4
// for the unstable_usePrompt implementation in react router itself
// and https://claritydev.net/blog/display-warning-for-unsaved-form-data-on-page-exit for yet another example
function Prompt(props) {
const block = props.when;
const onPageExit = props.onPageExit;
// NB gives warning in the browser console but thats a bug: https://github.com/remix-run/react-router/issues/10073
// apparently the fix of that bug caused a nr of bugs again too...
useBlocker(() => {
if (block()) {
const confirmed = window.confirm(props.message);
if (confirmed) {
onPageExit();
}
return !confirmed;
}
return false;
})
return (
<div />
);
}
export default Prompt;
Loading