packages/react-router/modules/Prompt.js
function Prompt({ message, when = true }) {
  return (
    <RouterContext.Consumer>
      {context => {
        invariant(context, "You should not use <Prompt> outside a <Router>");

        if (!when || context.staticContext) return null;

        const method = context.history.block;

        return (
          <Lifecycle
            onMount={self => {
              self.release = method(message);
            }}
            onUpdate={(self, prevProps) => {
              if (prevProps.message !== message) {
                self.release();
                self.release = method(message);
              }
            }}
            onUnmount={self => {
              self.release();
            }}
            message={message}
          />
        );
      }}
    </RouterContext.Consumer>
  );
}
function Prompt({ message, when = true }) {
  return (
    <RouterContext.Consumer>
      {context => {
        invariant(context, "You should not use <Prompt> outside a <Router>");

        if (!when || context.staticContext) return null;

        const method = context.history.block;

        return (
          <Lifecycle
            onMount={self => {
              self.release = method(message);
            }}
            onUpdate={(self, prevProps) => {
              if (prevProps.message !== message) {
                self.release();
                self.release = method(message);
              }
            }}
            onUnmount={self => {
              self.release();
            }}
            message={message}
          />
        );
      }}
    </RouterContext.Consumer>
  );
}
function Prompt ( { message , when = true } ) { return ( < RouterContext . Consumer > { context => { invariant ( context , "You should not use <Prompt> outside a <Router>" ) ; if ( ! when || context . staticContext ) return null ; const method = context . history . block ; return ( < Lifecycle onMount = { self => { self . release = method ( message ) ; } } onUpdate = { ( self , prevProps ) => { if ( prevProps . message !== message ) { self . release ( ) ; self . release = method ( message ) ; } } } onUnmount = { self => { self . release ( ) ; } } message = { message } / > ) ; } } < / RouterContext . Consumer > ) ; }
The public API for prompting the user before navigating away from a screen.
The public API for prompting the user before navigating away from a screen .