@@ -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 )
0 commit comments