Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
How to detect route change via browser back/forward button in MobX Router transition hook?

Imagine we have the following two routes:

viewRoute = {
    name: "View Route",
    pattern: "/route/view"
}

editRoute = {
    name: "Edit Route",
    pattern: "/route/edit",
    beforeExit: (fromState, toState, routerStore) => {
        if (/* route change was triggered by browser button */) {
            if (confirm("Do you want to discard changes?") {
                return toState;
            }
            else {
                return fromState;
            }
        }
    }
}

If the user redirects from "edit" to "view" route by clicking "cancel" or "save" buttons, I don't want to show the discard-confirm dialog. But if they use the browser buttons, I do.

How can I determine within a route transition hook whether that route change was triggered by a browser button? Or alternatively, by a direct call to routerStore.goTo()?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found a way to identify if a route was changed via button click, by leveraging RouterState.options to pass custom state to the transition hook.

From component:

const Component = () => {
    const cancelEditing = () => {
        routerStore.goTo("Edit Route", {}, {ignoreDiscard: true});
    }

    return <button onClick={cancelEditing}>{"Cancel"}</button>;
}

From Transition Hook:

beforeExit: (fromState, toState) => {
    if (!toState.options.ignoreDiscard) {
        if (confirm("Do you want to discard changes?") {
            return toState;
        }
        else {
            return fromState;
        }
    }
}

This state isn't persisted, so clicking browser back/forward buttons will always result in a dialog popup.

Unfortunately this method can only be achieved by calling RouterStore.goTo() directly as there's no prop on RouterLink called options.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda