old_contents: import React from "react";
  import PropTypes from "prop-types";
  import invariant from "invariant";
  
  import RouterContext from "./RouterContext";
  
  /**
   * The public API for prompting the user before navigating away
   * from a screen with a component.
   */
  class InnerPrompt extends React.Component {
    static defaultProps = {
      when: true
    };
  
    enable(message) {
      if (this.unblock) {
        this.unblock();
      }
  
      this.unblock = this.props.router.history.block(message);
    }
  
    disable() {
      if (this.unblock) {
        this.unblock();
        this.unblock = null;
      }
    }
  
    componentDidMount() {
      invariant(
        this.props.router,
        "You should not use <Prompt> outside a <Router>"
      );
  
      if (this.props.when) this.enable(this.props.message);
    }
  
    componentDidUpdate(prevProps) {
      if (this.props.when) {
        if (!prevProps.when || prevProps.message !== this.props.message) {
          this.enable(this.props.message);
        }
      } else {
        this.disable();
      }
    }
  
    componentWillUnmount() {
      this.disable();
    }
  
    render() {
      return null;
    }
  }
  
  function Prompt(props) {
    return (
      <RouterContext.Consumer>
        {router => <InnerPrompt {...props} router={router} />}
      </RouterContext.Consumer>
    );
  }
  
  if (__DEV__) {
    Prompt.propTypes = {
      when: PropTypes.bool,
      message: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired
    };
  }
  
  export default Prompt;
  
new_contents: import React from "react";
  import PropTypes from "prop-types";
  import invariant from "invariant";
  
  import RouterContext from "./RouterContext";
  
  class Block extends React.Component {
    constructor(props) {
      super(props);
      this.release = props.method(props.message);
    }
  
    componentDidUpdate(prevProps) {
      if (prevProps.message !== this.props.message) {
        this.release();
        this.release = this.props.method(this.props.message);
      }
    }
  
    componentWillUnmount() {
      this.release();
    }
  
    render() {
      return null;
    }
  }
  
  if (__DEV__) {
    Block.propTypes = {
      method: PropTypes.func.isRequired,
      message: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired
    };
  }
  
  /**
   * The public API for prompting the user before navigating away from a screen.
   */
  function Prompt(props) {
    return (
      <RouterContext.Consumer>
        {router => {
          invariant(router, "You should not use <Prompt> outside a <Router>");
  
          return props.when ? (
            <Block method={router.history.block} message={props.message} />
          ) : null;
        }}
      </RouterContext.Consumer>
    );
  }
  
  Prompt.defaultProps = {
    when: true
  };
  
  if (__DEV__) {
    Prompt.propTypes = {
      when: PropTypes.bool,
      message: PropTypes.oneOfType([PropTypes.func, PropTypes.string]).isRequired
    };
  }
  
  export default Prompt;
  
subject: Refactor <Prompt> and remove <InnerPrompt>