|
1 | 1 | // LIBRARY IMPORTS |
2 | 2 | import React, { Component } from "react"; |
3 | | -import { Route } from 'react-router-dom'; |
| 3 | +import { Route, Link } from 'react-router-dom'; |
4 | 4 |
|
5 | 5 | // STYLING |
6 | 6 | import "./Blog.css"; |
@@ -32,44 +32,51 @@ class Blog extends Component { |
32 | 32 | <header> |
33 | 33 | <nav> |
34 | 34 | <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> |
37 | 67 | </ul> |
38 | 68 | </nav> |
39 | 69 | </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 | | - } |
68 | 70 | <Route path="/" exact component={Posts} /> |
69 | 71 | <Route path="/new-post" component={NewPost} /> |
70 | 72 | </div> |
71 | 73 | ); |
72 | 74 | } |
73 | 75 | } |
74 | 76 |
|
| 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 | + |
75 | 82 | export default Blog; |
0 commit comments