Hey @ptmkenny, thanks for such a detailed reply!
I’ve looked through your code, and it looks we’re basically doing the same thing: returning a redirect if auth is false, otherwise returning the children (the page itself).
When you see pages re-render unexpectedly, it may be because a parent component is getting re-rendered when it shouldn’t be
I thought this might have hit the nail on the head, using context inside the protected routes is re-rendering them, instead of just ionic re-rendering the page without the user.
However, I switched to a simple useUser
hook to test, which just loads from localstorage, and the same issue persists.
const useUser = () => {
const user = localStorage.getItem("user");
const logIn = (user: string) => {
localStorage.setItem("user", user);
};
const logOut = () => {
localStorage.removeItem("user");
};
return { user, logIn, logOut };
};
Do you happen to know what your DOM looks like when logging in and out?
This is what mine looks like when using context and protected routes (and also the useUser hook):
And this is what it looks like when using a normal Route
and no auth guard (so never returning a Redirect
).
You can see in the first example the two protected pages being removed from the dom, as that route is now returning a Redirect
. Then, when I log back in, you can see them re-appearing (and this is when they animate in).
In the second example, they’re never removed from the dom, and so they never animate back in and everything works as intended.
I then thought it was because I was passing in ...rest
to my route, and the location was causing a re-render. So I removed that and only passed in path and exact.
I tried putting it inside a function like you did, inside the render method, but the same thing.
Either I don’t pass state, and ionic uses the cached version of the page (so I can log out, and still go to the feed page despite the returned redirect, because the route no longer gets called it seems because ionic has matched a component in the tree), or I use state and ionic re-renders all components in the tree when it gets changed, and I get the animating re-renders.
What does your log out look like? Does it update any state at the root of your app?