Author avatar

Desmond Nyamador

Understanding Links in React.js

Desmond Nyamador

  • Oct 8, 2020
  • 5 Min read
  • 94 Views
  • Oct 8, 2020
  • 5 Min read
  • 94 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Links are everywhere on the web. They are vital in locating resources such as web pages, images, and videos—not to speak of their importance in SEO. Routing helps determine which code should run when a URL is visited. In this guide, you will learn more about how routing works in React.

Links in React

In regular apps without a library like React, links are created with an anchor tag, as shown below.

1
<a href="https://pluralsight.com">Visit Pluralsight</a>
html

Bear in mind that this also works in React. This method of routing causes a full page refresh, when in reality the only thing that changed on the new route might be just an image and some text. In some cases, a full page refresh is useful, but in most cases, it's not needed. React is a component-oriented library and implements a neat algorithm by keeping track of your elements as a tree and figuring out which components need re-rendering. Libraries such as React Router take advantage of this to help your app render components that need re-rendering based on a specified route. Very efficient, isn't it?

In short, if your app uses the same header and footer for all pages, you can ensure that only the body of your pages re-render while the header and footer remain intact.

Building a Sample App

Setup a React app by entering the following command or entering react.new in your browser to set up a React development environment on codesandox.io.

1
npx create-react-app [YOUR_APP_NAME] && yarn add react-router-dom
bash

Add the following code block to your index.js file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import {Homepage, Aboutpage } from './App';

ReactDOM.render(
    <Router>
       <Switch>
		      <Route exact path="/" component={Homepage}/>
				<Route exact path="/about" component={Aboutpage }/>
	    </Switch>
    </Router>,
    document.getElementById('root')
);
jsx

Now add the following code snippet to your App.js file to create the components for the pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from 'react';
import { Link } from 'react-router-dom';

const Header = () => {
	return (
		<div>
				<p>Header</p>
		</div>
	)
};

const Homepage = () => {
	return (
		<div>
				<Header/>
				<h1>Homepage </h1>
				<Link to='/about'>Go to Aboutpage</Link>
		</div>
	)
};

const Aboutpage = () => {
	return (
		<div>
				<Header/>
				<h1>Aboutpage</h1>
				<Link to='/'>Go to Aboutpage</Link>
		</div>
	)
};
export {Homepage, Aboutpage } ;
jsx

Run your development server and open your app in the browser. Notice how changing routes does not cause a full page reload.

Cleaning Up to Avoid Unecessary Rendering

You've made use of routes to render specific components for each route. But there's a problem somewhere 🤔. The </Header> component has to render every time a route changes, but its content doesn't change. Edit your App.js and index.js files, respectively, as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import {Homepage, Aboutpage, Header } from './App'; //new

ReactDOM.render(
    <Router>
       <Switch>
					<Header/> //new
		      <Route exact path="/" component={Homepage}/>
				<Route exact path="/about" component={Aboutpage }/>
	    </Switch>
    </Router>,
    document.getElementById('root')
);
jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from 'react';
import { Link } from 'react-router-dom';

const Header = () => {
	return (
		<div>
				<p>Header</p>
		</div>
	)
};

const Homepage = () => {
	return (
		<div>
				<h1>Homepage </h1>
				<Link to='/about'>Go to Aboutpage</Link>
		</div>
	)
};

const Aboutpage = () => {
	return (
		<div>
				<h1>Aboutpage</h1>
				<Link to='/'>Go to Aboutpage</Link>
		</div>
	)
};
export {Homepage, Aboutpage , Header } ; //new
jsx

What you've done is you've made sure that when a new route is visited, it only renders the respective page and not the header, as it seldom changes state.

Conclusion

Whew! That's it. You should now get the concept of links in React and how to handle routing effectively.

Feel free to ping me on twitter @DesmondNyamador if you want chat more on this topic.

2