question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: Route match params not readable in lifecycle hooks in Ionic React

See original GitHub issue

It seems that the route match params do not get updated in the Ionic-React lifecycle hooks.

Ionic version: [ ] 4.x [x] 5.x. @ionic/react: 5.2.2

Current behavior: When a route is visited again, Ionic reuses the existing page. This is a bit different from how React does it, but as described in the docs there are handy lifecycle hooks available to help with it.

However the route match params don’t seem to update.

Expected behavior: I would expect that the match params are updated.

Steps to reproduce:

Some pseudo-code to show the issue:

  <IonApp>
     <IonReactRouter>
       <IonRouterOutlet>
              <Route path="/home" component={Home} exact />
              <Route path="/game/:id" component={GamePage} exact />
        </IonRouterOutlet>
      </IonReactRouter>
    </IonApp>

interface GameProps
  extends RouteComponentProps<{
    id: string
  }> {}

const GamePage: React.FC<GameProps> = ({ match }) => {

useIonViewDidEnter(() => {
    console.log('useIonViewDidEnter ', match.params.id) // always the initial id
})
useEffect(() =>
    console.log('useEffect', match.params.id) // always the current id
})
return <IonPage><IonContent>{ match.params.id }</IonContent></IonPage>

In another part of the app:

history.push('/game/10');

and later

history.push('/game/11');

But

console.log('useIonViewDidEnter ', match.params.id) // always the initial id

keeps outputting 10 (the first)

Related code: If needed I can create a sample project.

Ionic info:


Ionic:

   Ionic CLI       : 6.10.1
   Ionic Framework : @ionic/react 5.2.2

Capacitor:

   Capacitor CLI   : 2.2.0
   @capacitor/core : 2.2.0

Utility:

   cordova-res (update available: 0.15.0) : 0.14.1
   native-run                             : 1.0.0

System:

   NodeJS : v14.3.0
   npm    : 6.14.5
   OS     : macOS Catalina


Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
avalanche1commented, Jun 1, 2021

Can we raise the priority for this bug? It’s been a year with no fix - and it’s a serious issue - Ionic lifecycle hooks not working as expected - what’s the point of having them then?..

1reaction
hansvncommented, Oct 15, 2021

@kaloczikvn can you try this:

const getId = useCallback(() => {
  return match.params.id;
}, [match.params]);

useIonViewDidEnter(() => {
    console.log("useIonViewDidEnter ", getId());
});

I’ve come across this bug a couple of times, and my understanding is that useIonViewDidEnter falls outside of the scope of the Functional Component. Calling a method inside the scope should return the correct id (not too sure if you’ll actually need the callback, I’m writing this from the top of my head but this should be the most robust solution at this point).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ionic React props.match.params doesn't update - Stack Overflow
It turned out that it was Ionics lifecycle method that was the problem. My solution was to make a useEffect instead like this:...
Read more >
Error with Params on first load of page on Real Device
I have the issue following links using or using onClick={ () => {history.push ('/groups/xxxx'); }, the issue is that I can't read parameter...
Read more >
Lifecycle hooks - Angular
A component instance has a lifecycle that starts when Angular instantiates the component class and renders the component view along with its child...
Read more >
Ionic 4 Beta: What's New and Building a Sample App - Auth0
Angular Router: As mentioned earlier, you need to migrate your Push/Pop navigation to Angular Router navigation. · NavParams Service: With ...
Read more >
Angular 2 - Redirect to Previous URL after Login with Auth ...
2) Navigate to the login component by clicking in a link - not by typing the URL in the browser bar. Don't log...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found