Skip to content

Commit 402674b

Browse files
committed
added route with properties
1 parent fd68264 commit 402674b

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,63 @@ ReactDOM.render(
5252
document.getElementById('root'));
5353
```
5454

55-
The goal for Hash Router is to keep our user interface in sync with what ther URL is. Meaning, that will display specific components on what the URL in our browser is.
55+
The goal for Hash Router is to keep our user interface in sync with what ther URL is. Meaning, that will display specific components on what the URL in our browser is.
56+
57+
### Route
58+
59+
Route is the component we will use from `react-router-dom` to determine a specific route for what component will be displayed based on the URL.
60+
61+
We first need to import it into our file to use.
62+
63+
```js
64+
import {Route} from 'react-router-dom';
65+
```
66+
67+
> Make sure to use object destructering!
68+
69+
We can now use `Route` inside of our JSX and give it specific props to determine what component is displayed based off of the URL path.
70+
71+
```js
72+
<Route />
73+
```
74+
75+
#### Component
76+
77+
The `component` prop is the prop we can add to determine what component is displayed.
78+
79+
```js
80+
<Route component={Home} />
81+
```
82+
83+
Above we are rendering a the Home component for this route. Now we just need to determine what the URL path or route will be to display this component.
84+
85+
#### Path
86+
87+
The `path` prop will determine what the URL path should be to display the component.
88+
89+
```js
90+
<Route path='/home' component={Home} />
91+
```
92+
93+
Above, we have determined that if the URL ends with '/home' then the Home component will be displayed.
94+
95+
#### Exact
96+
97+
Another prop that we can add to our Route is `exact`. This prop will tell the route that the URL needs to match up exactly with the path we defined on our Route.
98+
99+
```js
100+
<Route exact path='/home' component={Home} />
101+
```
102+
103+
Above, we have declared the URL needs to end exactly with '/home'. If we have anything following or before the '/home' then the component will not be displayed.
104+
105+
This is extremely important to include if we want our base route to be displayed correctly. We can determine what our base route is by creating a route for '/'.
106+
107+
```js
108+
// bad
109+
<Route path='/' component={Home} />
110+
111+
// good
112+
<Route exact path='/' component={Home} />
113+
```
114+

src/App.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import React, { Component } from 'react'
22

3+
// React Router
4+
import {Route} from 'react-router-dom';
5+
6+
// Components
7+
import Home from './components/Home';
8+
39
class App extends Component {
410
render() {
511
return (
612
<div>
7-
13+
<Route path='/home' component={Home} />
814
</div>
915
)
1016
}

src/components/Home.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Home = () => <h1>Home Route</h1>
4+
5+
export default Home;

0 commit comments

Comments
 (0)