Skip to content
Discussion options

You must be logged in to vote

Ok - I found a solution that works for me good.
Maybe some has a better Solution.

import { useEffect } from 'react';
import { unstable_useBlocker as useBlocker } from 'react-router-dom';

function Prompt(props) {
  const block = props.when;

  useBlocker(() => {
    if (block) {
      return !window.confirm(props.message);
    }
    return false;
  });

  useEffect(() => {
    const handleBeforeUnload = (event) => {
      if (block) {
        event.preventDefault();
        event.returnValue = '';
      }
    };

    const handleUnload = () => {
      if (block) {
      }
    };

    window.addEventListener('beforeunload', handleBeforeUnload);
    window.addEventListener('unload', handleU…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tonyxuan2021
Comment options

Answer selected by MichaelDeBoey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants