-
Notifications
You must be signed in to change notification settings - Fork 13.5k
bug: Route match params not readable in lifecycle hooks in Ionic React #21594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the issue! This issue has been labeled as Please provide a reproduction with the minimum amount of code required to reproduce the issue. Without a reliable code reproduction, it is unlikely we will be able to resolve the issue, leading to it being closed. For a guide on how to create a good reproduction, see our Contributing Guide. |
See this repository for a minimal example, check Detail.tsx and the console.logs in there |
Hi @JanMisker, Thanks for the repo. I just gave it a try and wasn't seeing the issue. Here is what I did: After loading, I hit the Push 10 button, and went into the details page. The console showed:
I hit the back button, updated the text field to 101, and hit the Push 101 button, when it went to the details page the console showed:
From what I gathered, the bug you were saying was that the ionViewDidEnter was reporting the id was still "10" correct? Let me know if I'm missing something. |
You're right, I didn't notice that the back button seems to work ok. This is exactly the situation for which I would use the Ionic lifecycle hooks: Ionic keeps the view mounted, but I do want to know when it is being presented again, with up-to-date info on the parameters. The lifecycle hook documentation also specifies that it can happen like that.
|
@JanMisker Yep, see it now. Will take a look, thanks! |
Hi @JanMisker, Found this issue, you need to pass the param to useIonViewWillEnter in the dependencies list much like useEffect:
Give that a try and let us know. And sorry, I don't think thats documented, so we will get that updated. |
I have the same exact issue. |
Thanks @elylucas that indeed fixed it! @tottt check out my reproduction repository, maybe your case is different somewhere? https://github.com/JanMisker/routeParamsBug |
@elylucas come to think of it... doesn't the requirement to add a dependency array defy the usefulness of those lifecycle hooks? |
My case is quite similar to your example. |
The function still gets called for each enter/leave, but it only updates the value of the variables captured in the closure that are listed in the deps array. |
@tott could you put together a demo repo? |
I have also created a demo repo: https://bitbucket.org/dodosch/routebugdemo
When you use the back button it works fine. Interestingly when you repeat step 2 and 3 it works again until you got to step 1 - then it breaks again. |
K, I see it and will take a look. In the meantime, I think you could replace the lifecycle hook with a useEffect hook to get the right param in. |
Ionic react, lifecycle hooks do not work correctly when routing with params is used! Example: http://localhost:8100/dashboard/2 But useIonViewWillLeave and useIonViewDidLeave are never called! Did not yet find a workaround for this! |
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?.. |
Still no mention of this in the documentation plus I'm still stuck with this issue. |
@kaloczikvn can you try this:
I've come across this bug a couple of times, and my understanding is that |
@hansvn: How can I use this inside a React Context? I can also move that context inside the Router components, if this is needed. But a React Context doesn't have |
What I've learned meanwhile is that you can pass some props to the second argument of the useIonViewDidEnter(() => {
...
}, []); |
@hansvn: Yes, interface ViewCategoryProps
extends RouteComponentProps<{
category: string;
}> { }
const ViewCategory: React.FC<ViewCategoryProps> = ({ match }) => {
const categorySlug = match.params.category; For triggering something when a page is visited (e.g. "Order sending" page), I currently use the onClick={() => {
orderCtx.sendOrder();
history.push({
pathname: '/send',
});
}} |
Any update on this issue? Our team is experiencing this problem as well, and I can see that it's already been a while, and so far I haven't seen any fixes. @mhartington maybe you could have one of your team mates take a look at this issue? Our team has been struggling with this for a like 2-3 weeks already. Many thanks! :) |
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:
In another part of the app:
and later
But
keeps outputting 10 (the first)
Related code:
If needed I can create a sample project.
Ionic info:
The text was updated successfully, but these errors were encountered: