Sitemap

<Link> Component in React

Oct 5, 2021

What is <Link>? How does <Link> work? How do I use <Link>? Why so many questions?

No 😔Unfortunately not that Link.

React provides a declarative component for redirecting within a Single Page Application. The <Link> Component works by using a string as an accessible navigator to different parts of your webpage, by concatenating the pathway you have assigned it. It looks a little something like this:

Press enter or click to view image in full size
The code!
Press enter or click to view image in full size
How it renders on the page!
Press enter or click to view image in full size
What the “Book Me!” Hyperlink redirects to — notice the “/contact” pathway in the URL.

Don’t forget to first import <Link> at the top of every Component.js file like so:

Press enter or click to view image in full size

Then the to=string portion of the <Link> must be the pathway as defined within your React Router Components in the App.js file, like so:

Press enter or click to view image in full size
In the specific Component file
Press enter or click to view image in full size
In the App.js file

And there you have it!

Further Reading:

--

--

Sam Hall
Sam Hall

Written by Sam Hall

Full stack developer and stand-up comic.

No responses yet