Hello, in this tutorial, we'll go over Route Layouts and the Outlets component from react-router-dom.
Hello, in this tutorial, we'll go over Route Layouts and the Outlets component from react-router-dom.
Route Layouts
Keep in mind that we'll be using what we did in the Routing tutorial so be sure to do that first.
Step 1:
In the App component from the Routing tutorial, wrap the "/" and "/page2" routes with another Route component.
Step 2:
Step 3:
Note: If you save and launch the app. The components will not show.
Step 4:
import { Outlet } from 'react-router-dom';
Step 5:
The Outlet component is similar to insert the children prop, except it will display whatever is in the active route.
If you go to localhost:3000, you should what the contents of MyFirstComponent now.
Adding Shared Elements
Now that you have the Page Layout component, we can add more HTML to it and it'll be present in the "/" or "/page2" routes.
Step 2:
Now if we save and go to localhost:3000, we should see the <h1>, <Link>, and <p> elements.
If we click on the Go to Page 2 link, we should still see the <h1> and <p> element but the content in the middle will change.
That is all for Router Layouts and the Outlet component. Now you can easily share HTML or components between each route.