Use the <Link> component
In the last lesson we saw that state.router.link
stores the current URL. We demonstrated this by manually changing the URL in the browser's address bar and saw how the URL in our <p>
element changed as the component reacted to the data change.
In this lesson we will create a menu-based navigation to change the URL for us.
Frontity provides a number of useful components for us, amongst which is the <Link>
component. The simplest way to use it is to just provide a link
attribute with the URL as its value:
We also provide the string that we want to be displayed as a clickable link. In the example above that is 'Home'.
The <Link>
component simply outputs an <a>
element into the resulting HTML, but without forcing a page reload which is what would occur if you simply added an <a>
element instead of using the <Link>
component.
Before we can use any of Frontity's built-in components we first need to import it. Frontity's built-in components are imported from @frontity/components
.
So let's import the <Link>
component into our root component file for use within our <Root>
component. Let's add this import statement as the third line:
With that done we are now in a position to add a menu consisting of a <nav>
element with three <Link>
items as children. Using the example above as a template our index.js
file can now look like this:
When you save the file the browser should update and the menu will now be visible. Now if you click on the menu items you will see the URL in the browser's address bar change, and also the displayed value of state.router.link
.
Here we've used the <Link>
component in the simplest possible way. To learn more about what you can do with the <Link>
component, including pre-fetching strategies to make your site more responsive, see this page in our documentation.
Check you're on the right track by comparing your changes with the ones in this commit.
Last updated