Skip to content

bug: Hardware back event triggered twice (or more) if an IonReactRouter is unmounted and re-mounted #23170

@Cyral

Description

@Cyral

Bug Report

Ionic version:

[ ] 4.x
[x] 5.x

Current behavior:
Hardware back button triggers the back event multiple times.

Expected behavior:
Hardware back button only triggers event once

Steps to reproduce:
Create a React app with two IonReactRouters.

For example, one router for logged out users and one for logged in users. The logged out router is mounted when the user is logged out, and upon login the new router is mounted. Only one router is mounted at any given moment, but users who log in or out will cause more than one router to be created during the app lifetime.

For example: {isLoggedIn ? <AuthedApp/> : <UnauthedApp/>} where each component has it's own routing.

Navigate to at least two pages in the second router and then press back. Ionic will take you back two pages instead of one.

Related code:

I think the issue is that the ionBackButton event is registered but never removed when the router is unmounted. It should be removed when componentWillUnmount is called.

Also seeing Can't perform a React state update on an unmounted component. from IonReactRouter which further points to this being the issue.

Other information:

Ionic info:

Ionic:

   Ionic CLI       : 6.12.3 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework : @ionic/react 5.6.3

Capacitor:

   Capacitor CLI   : 2.4.7
   @capacitor/core : 2.4.7

Utility:

   cordova-res (update available: 0.15.3) : 0.15.2
   native-run (update available: 1.3.0)   : 1.2.2

System:

   NodeJS : v14.15.0 (/usr/local/Cellar/node@14/14.15.0/bin/node)
   npm    : 6.14.8
   OS     : macOS Big Sur

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions