Skip to content

Commit 1c46d5e

Browse files
committed
Efficient Way to re-route - The Page doesn't reload using <Link to="/">
Component from 'react-router-dom'
1 parent 2ccf46b commit 1c46d5e

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,6 @@ Required Dependency: **`npm i --save axios`** | **[`axios` DOCS](https://github.
129129
3. Preparing the Project for Routing &mdash; Making `<NewPost />`, `<FullPost />` & `<Posts />` Components as a Container for Proper Routing: [Commit Details](https://github.com/Ch-sriram/react/commit/e8d0a46e2892af940f600481cba861393dca051b)
130130
4. Setting Up and Rendering Routes using `path`, `exact` & `render` attributes inside the `<Route />` Component: [Commit Details](https://github.com/Ch-sriram/react/commit/0ae8809410db1d704c99ab3b61a7a6d8e906b0f2)
131131
5. Rendering Components for Routes using `render` and `component` prop inside the `<Route />` Component: [Commit Details](https://github.com/Ch-sriram/react/commit/9c39b5454c672cc7d8a1868190958d9ca9dca688)
132-
6. Switching between Pages using Links (*Inefficient Way to Re-route* &mdash; Entire Page Reloads): [Commit Details](https://github.com/Ch-sriram/react/commit/c5ab53754f934db1ea3fd2dba86d4e6cf4f7e7e5)
132+
6. Switching between Pages using Links
133+
1. *Inefficient Way to Re-route* &mdash; Entire Page Reloads using `<a href="/">Home</a>` Component: [Commit Details](https://github.com/Ch-sriram/react/commit/c5ab53754f934db1ea3fd2dba86d4e6cf4f7e7e5)
134+
2. Efficient Way to Re-route &mdash; The Page Doesn't Reload using `<Link to="/">Home</Link>` Component: [Commit Details]()
Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// LIBRARY IMPORTS
22
import React, { Component } from "react";
3-
import { Route } from 'react-router-dom';
3+
import { Route, Link } from 'react-router-dom';
44

55
// STYLING
66
import "./Blog.css";
@@ -32,44 +32,51 @@ class Blog extends Component {
3232
<header>
3333
<nav>
3434
<ul>
35-
<li><a href="/">Home</a></li>
36-
<li><a href="/new-post">New Post</a></li>
35+
{
36+
/**
37+
* Now, this <Link to="/"> Component below will
38+
* not reload the entire page, whereas, it will
39+
* just render the component which is given in
40+
* the path using the `to` prop.
41+
*
42+
* The `to` prop can also be a more complex
43+
* element, which can be an entire JS object,
44+
* that takes in a config which can be as
45+
* follows:
46+
* {
47+
* pathname: '/new-post',
48+
* hash: '#submit',
49+
* search: `?quick-submit=true',
50+
* }
51+
* where, `pathname` is the route address,
52+
* `hash` is used for going to a specified
53+
* id inside the route we mentioned
54+
* above in `pathname`.
55+
* `search` is for query parameters to the
56+
* link in the address bar.
57+
*
58+
* Example shown below.
59+
*/
60+
}
61+
<li><Link to="/">Home</Link></li>
62+
<li><Link to={{
63+
pathname: '/new-post',
64+
hash: '#submit',
65+
search: '?quick-submit=true',
66+
}}>New Post</Link></li>
3767
</ul>
3868
</nav>
3969
</header>
40-
{
41-
/**
42-
* Only issue we've right now is that, every time we
43-
* click on the links we defined above, which are
44-
* "Home" & "New Post", what we are doing is, we are
45-
* re-loading our entire application each time we
46-
* click the links.
47-
*
48-
* This might not seem like a problem here, but
49-
* theoretically, a reload means, we are reloading
50-
* the entire JS related to the website, again, which
51-
* is just a massive waste of time, and because of
52-
* this, our previous application state is also lost,
53-
* which we don't want.
54-
*
55-
* As long as the user is navigating around inside
56-
* the same HTML page, we never want to reload the
57-
* entire page again, we just want to re-render the
58-
* page in the parts where we need to render the
59-
* components, so that it just looks like a totally
60-
* new page, without even rendering anything onto the
61-
* page.
62-
*
63-
* Therefore, next time, we will see how to stay
64-
* inside the same page and not reload the entire
65-
* page again when trying to route to a new component
66-
*/
67-
}
6870
<Route path="/" exact component={Posts} />
6971
<Route path="/new-post" component={NewPost} />
7072
</div>
7173
);
7274
}
7375
}
7476

77+
/**
78+
* Now our app won't reload every time we click on the links
79+
* aforementioned in the "Home" and "New Post" links.
80+
*/
81+
7582
export default Blog;

0 commit comments

Comments
 (0)