

Navigate(-2) // go to the page before the previous page.įorward and backward navigation is also possible with this navigate function. Navigate(-1) // to go back to the previous page. Import ) Īlso, besides passing a path in navigate, we can also pass the numbers. Because it was clear when we had the route definition in the nested component, now we moved that route to app.js, react router not have clear understanding where exactly the nested route should be added or render.įor this, react router v.6 introduced another new component called Outlet, which we can import from react-router-dom package. The above code look very easy to track the route, also we can define all the Routes in a single place.īut, if we use this pattern of route define, we have to tell react router where this nested content should be inserted into the DOM. We can even improve the code by doing this nested route in app.js component. We had seen above, how to do the proper nested routes in v.6. Therefore, Links and Routes are Relative, if they are loaded inside of nested route component, because that route is already loaded because of another route. This new change is not difficult to update from v.5, actually it is a good change, because we can keep our nested route path shorter, and we don’t need o repeat the entire fill path every time.Īs we have already seen, in v.5 we need to provide full path in to-props of Link, but in v.6, since this Link exist inside of the nested route component, to-props value is relative as well. We don’t need to repeat full path /component1/component2 anymore. Hence, we have to add only the extra part, the extra elements in the path that are relevant for the route, not full path. In component1 we have used nested routes, therefore the route path in component1 already assumes, that we have /component1 at the beginning. To make component2 render, first we have to embrace this new syntax “/*” in the parent route.Ībove code component1 route will become active if the path starts with /component1.

So, according to the above example code, route will render component1, but it wont render component2 in v.6. The above nested route method will work fine in v.5, but in v.6 this won't work, in v.6 the logic and syntax we needed for nested routes is changed, it's actually changed for the better.Īs we already mentioned in part 1 article, we can add “/*” to React Router and it should match the route for the start of the path instead of the full path. Routes are RelativeĪccording to v.5, we’ll use nested routes as follows,

In the last article we end up with Nested Route, let's continue with some extra points with Nested Route. Please visit the previous article if you are in the beginning stage of migrating React Router v.5 to v.6 This article is the continuation of React-router-v6 what changed and Upgrading guide, React-router-v6 what changed and Upgrading guide - part-2 articles.
