Skip to content

Commit a171c56

Browse files
committed
finished notes
1 parent 402674b commit a171c56

6 files changed

Lines changed: 145 additions & 2 deletions

File tree

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,88 @@ This is extremely important to include if we want our base route to be displayed
112112
<Route exact path='/' component={Home} />
113113
```
114114

115+
### Switch
116+
117+
Switch is another component that we can use from react router dom library to render only the first route that matches the URL.
118+
119+
If we had multiple routes like so
120+
121+
```js
122+
<Route exact path='/' component={Home} />
123+
<Route path='/about' component={About} />
124+
<Route path='/about/contact' component={Contact} />
125+
```
126+
127+
If we went to '/about/contact' we would still see the component that is being rendered at '/about' because the URL still matches. We could fix this including a bunch of `exact` props, or we could use `Switch`.
128+
129+
We can use `Switch` by wrapping all of our `Route` components inside of it. We need to make sure we also import Switch into our file
130+
131+
```js
132+
import {Route, Switch} from 'react-router-dom';
133+
```
134+
135+
> Remember to use the object destructering!
136+
137+
Now once we have it imported, we can use it
138+
139+
```js
140+
<Switch>
141+
<Route exact path='/' component={Home} />
142+
<Route path='/about' component={About} />
143+
<Route path='/about/contact' component={Contact} />
144+
</Switch>
145+
```
146+
147+
We need to make sure that our Routes are in oreder inside of our Switch, since it looks for the first route that matches the URL.
148+
149+
### Link
150+
151+
We can navigate to different routes inside of our application by using the `Link` component that comes from react-router-dom.
152+
153+
We first need to import the `Link` into our file
154+
155+
```js
156+
import {Route, Switch, Link} from 'react-router-dom';
157+
```
158+
159+
Then we will wrap what ever we want to click on to take us to the route.
160+
161+
```js
162+
<Link><p>Home</p></Link>
163+
```
164+
165+
#### To
166+
167+
The `Link` component has a required prop called `to`. This is how we will tell what route the link should take us to.
168+
169+
```js
170+
<Link to='/'><p>Home</p></Link>
171+
```
172+
173+
Above we are now telling this `Link` tag to take us to the base route, which will render the Home component.
174+
175+
### Params
176+
177+
#### Params
178+
179+
We can set `params` inside of our path by prefixiing the part of the path with a colon. This will allow us to pass data to our path.
180+
181+
```js
182+
<Route path='/users/:id' component={User} />
183+
```
184+
185+
Above we are saying that whatever is passed to the '/:id' will be in our URL. We can link to it like so
186+
187+
```js
188+
<Link to='/users/5'><p>User Profile</p></Link>
189+
```
190+
191+
Above, we are passing the number 5 to the URL. We can access that data that is being sent through the URL from the `props.match.params` object.
192+
193+
```js
194+
const {id} = props.match.params;
195+
```
196+
197+
or if we console logged `props`, the object would look like the following
198+
199+
![props object](images/props)

images/props.png

107 KB
Loading

src/App.js

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

33
// React Router
4-
import {Route} from 'react-router-dom';
4+
import {Route, Switch} from 'react-router-dom';
55

66
// Components
77
import Home from './components/Home';
8+
import About from './components/About';
9+
import Contact from './components/Contact';
10+
import Navbar from './components/Navbar';
811

912
class App extends Component {
1013
render() {
1114
return (
1215
<div>
13-
<Route path='/home' component={Home} />
16+
<Navbar />
17+
<Switch>
18+
<Route exact path='/' component={Home} />
19+
<Route path='/about' component={About} />
20+
<Route path='/contact' component={Contact} />
21+
</Switch>
1422
</div>
1523
)
1624
}

src/components/About.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
3+
const About = (props) => {
4+
console.log(props)
5+
return <h1>About Route</h1>
6+
}
7+
8+
export default About;

src/components/Contact.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 Contact = () => <h1>Contact Route</h1>
4+
5+
export default Contact;

src/components/Navbar.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
3+
// Import Link
4+
import {Link} from 'react-router-dom';
5+
6+
// Style obj for nav
7+
const navStyles = {
8+
width: '100vw',
9+
height: '10vh',
10+
background: 'tomato',
11+
display: 'flex',
12+
flexDirection: 'row-reverse',
13+
alignItems: 'center'
14+
};
15+
16+
const linkStyle = {
17+
color: 'white',
18+
fontSize: '20px',
19+
marginRight: '10px',
20+
textDecoration: 'none'
21+
};
22+
23+
const Navbar = () => (
24+
<nav style={navStyles}>
25+
<Link to="/" style={linkStyle}>
26+
<span>Home</span>
27+
</Link>
28+
<Link to="/about" style={linkStyle}>
29+
<span>About</span>
30+
</Link>
31+
<Link to="/contact" style={linkStyle}>
32+
<span>Contact</span>
33+
</Link>
34+
</nav>
35+
)
36+
37+
export default Navbar;

0 commit comments

Comments
 (0)