modules/createTransitionManager.js
function listenBeforeLeavingRoute(route, hook) {
    // TODO: Warn if they register for a route that isn't currently
    // active. They're probably doing something wrong, like re-creating
    // route objects on every location change.
    const routeID = getRouteID(route)
    let hooks = RouteHooks[routeID]

    if (!hooks) {
      let thereWereNoRouteHooks = !hasAnyProperties(RouteHooks)

      RouteHooks[routeID] = [ hook ]

      if (thereWereNoRouteHooks) {
        // setup transition & beforeunload hooks
        unlistenBefore = history.listenBefore(transitionHook)

        if (history.listenBeforeUnload)
          unlistenBeforeUnload = history.listenBeforeUnload(beforeUnloadHook)
      }
    } else {
      if (hooks.indexOf(hook) === -1) {
        warning(
          false,
          'adding multiple leave hooks for the same route is deprecated; manage multiple confirmations in your own code instead'
        )

        hooks.push(hook)
      }
    }

    return function () {
      const hooks = RouteHooks[routeID]

      if (hooks) {
        const newHooks = hooks.filter(item => item !== hook)

        if (newHooks.length === 0) {
          removeListenBeforeHooksForRoute(route)
        } else {
          RouteHooks[routeID] = newHooks
        }
      }
    }
  }
function listenBeforeLeavingRoute(route, hook) {
    // TODO: Warn if they register for a route that isn't currently
    // active. They're probably doing something wrong, like re-creating
    // route objects on every location change.
    const routeID = getRouteID(route)
    let hooks = RouteHooks[routeID]

    if (!hooks) {
      let thereWereNoRouteHooks = !hasAnyProperties(RouteHooks)

      RouteHooks[routeID] = [ hook ]

      if (thereWereNoRouteHooks) {
        // setup transition & beforeunload hooks
        unlistenBefore = history.listenBefore(transitionHook)

        if (history.listenBeforeUnload)
          unlistenBeforeUnload = history.listenBeforeUnload(beforeUnloadHook)
      }
    } else {
      if (hooks.indexOf(hook) === -1) {
        warning(
          false,
          'adding multiple leave hooks for the same route is deprecated; manage multiple confirmations in your own code instead'
        )

        hooks.push(hook)
      }
    }

    return function () {
      const hooks = RouteHooks[routeID]

      if (hooks) {
        const newHooks = hooks.filter(item => item !== hook)

        if (newHooks.length === 0) {
          removeListenBeforeHooksForRoute(route)
        } else {
          RouteHooks[routeID] = newHooks
        }
      }
    }
  }
function listenBeforeLeavingRoute ( route , hook ) { // TODO: Warn if they register for a route that isn't currently // active. They're probably doing something wrong, like re-creating // route objects on every location change. const routeID = getRouteID ( route ) let hooks = RouteHooks [ routeID ] if ( ! hooks ) { let thereWereNoRouteHooks = ! hasAnyProperties ( RouteHooks ) RouteHooks [ routeID ] = [ hook ] if ( thereWereNoRouteHooks ) { // setup transition & beforeunload hooks unlistenBefore = history . listenBefore ( transitionHook ) if ( history . listenBeforeUnload ) unlistenBeforeUnload = history . listenBeforeUnload ( beforeUnloadHook ) } } else { if ( hooks . indexOf ( hook ) === - 1 ) { warning ( false , 'adding multiple leave hooks for the same route is deprecated; manage multiple confirmations in your own code instead' ) hooks . push ( hook ) } } return function ( ) { const hooks = RouteHooks [ routeID ] if ( hooks ) { const newHooks = hooks . filter ( item => item !== hook ) if ( newHooks . length === 0 ) { removeListenBeforeHooksForRoute ( route ) } else { RouteHooks [ routeID ] = newHooks } } } }
Registers the given hook function to run before leaving the given route.

During a normal transition, the hook function receives the next location
as its only argument and must return either a) a prompt message to show
the user, to make sure they want to leave the page or b) false, to prevent
the transition.

During the beforeunload event (in browsers) the hook receives no arguments.
In this case it must return a prompt message to prevent the transition.

Returns a function that may be used to unbind the listener.
Registers the given hook function to run before leaving the given route .