When a page is in an IonRouterOutlet , the container controls the transition animation between the pages as well as controls when a page is created and destroyed, which helps maintain the state between the views when switching back and forth between them. – source
So if I’m editing a birthday (EditBirthday), and then go to the page for viewing a birthday (ViewBirthday), and then delete that same birthday… then I have an error thrown because the EditBirthday page’s data for that deleted birthday is no longer accessible.
A snippet of the EditBirthday page looks like this…
birthday becomes undefined, so accessing properties of it throws a TypeError.
So what I really need is for the EditBirthday page to not save state between views. Or how do I actually destroy a page like this one once done with it?
Maybe you could use history.replace() instead of history.push(), but it depends on how the user can navigate from one page to another, if it’s a link in one page or tabs or menu or back button…
Incidentally, /birthday/:id also matches /birthday/new, it’s not an exclusive match.
But how does the user navigate between EditBirthday and ViewBirthday?
IonRouterOutlet is not like a Switch, if multiple components match the requested path they will all be rendered. You can easily verify this by adding a console.log('rendering ComponentName') at the top of each component function.
Make sure the various paths do not overlap with each other. I’m not aware of another way.
What IonRouterOutlet gives you is the transition effects when navigating between pages. If you don’t need those transitions for some routes you can use a regular Switch.
Once a user has edited a birthday (EditBirthday), and then clicks Update, once the updates have been made, I now call history.replace('/home'); per your earlier suggestion. I actually decided to replace all history.push() with history.replace()
i think the bigger question might be how you are architecting your user experience, this is a pretty standard CRUD UI around adding data, it should be list click push to detail, back, list push to add, back
Try history.goBack(). But @aaronksaunders is right, after adding an entry you should just goBack to the Home page.
You’ll need replace if you navigate from ViewBirthday to EditBirthday, which frankly I’m still not sure exactly how you’ve designed.
But it wouldn’t be an unreasonable flow to have e.g. List > View Item > Edit Item and the edit page also allow the user to delete the item. In that case you’d want to replace the View Item with the Edit Item, otherwise the View Item will be kept in the navigation stack.
The design is… view a list of birthdays, then clicking a birthday, you can either edit or delete that birthday. In EditBirthday once changes are saved, it goes back to ViewBirthday.
So from what I can tell, just using history.goBack() does the job, and I don’t actually need to use history.replace anymore.
@magician1111 & @mirkonasato : We had a similar problem with this behaviour of IonRouterOutlet but since it uses react-router-dom's Route you can use regex in the path prop if you prefere the /birthday/new and birthday/:id paths, which would look like this (if :id is a number, otherwise you have to figure out a regex for new): <Route path="/birthday/:id(\d+)" component={ViewBirthday} exact />