Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic React props.match.params doesn't update

I'm building a site to display real estate with React and Ionic.

I have this routing in my App.tsx

<IonApp>
  <IonReactRouter>
    <Nav />
    <IonRouterOutlet id="first">
      <Route exact path="/" component={Home} />
      <Route exact path="/all-properties" component={Properties} />
      <Route exact path="/property/:id" component={Property} />
    </IonRouterOutlet>
  </IonReactRouter>
</IonApp>

And I link to the single "/property/:id" page in my website like this:

<Link to={"/property/" + variableId}> ... </Link>

Now on my "/property/:id" page I can access the id from the URL like this:

useIonViewDidEnter(() => {
  console.log(props.match.params.id); // This works the first time
});

The problem is, when I click back to say the home screen, and then visit another of my "/property/:id" pages, the props.match.params.id stays the same. It doesn't update.

What could be done to fix it?

like image 577
Julius Avatar asked Dec 03 '25 17:12

Julius


1 Answers

It turned out that it was Ionics lifecycle method that was the problem.

My solution was to make a useEffect instead like this:

useEffect(() => {
   console.log(props.match.params.id)
}, [props.match.params.id])
like image 191
Julius Avatar answered Dec 06 '25 08:12

Julius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!