- Add Routing between 2 pages to react
- Use
Linkto changeurls - Access values from the url to a url and pass it to a component
- Pass data to route components
- Manage page travesal programatically
- Use
<Redirect />to move from route to route - Explain how using redux changes the architecture of a react project that uses routes
- https://github.com/rogerwschmidt/react-router-no-api
- https://github.com/rogerwschmidt/react-router-passing-data
- Install package
npm install react-router-dom
- Add
BrowserRouterinApp.js
import { BrowserRouter } form 'react-router-dom
- Add
<BrowserRouter></BrowserRouter>toApp.js
class App extends Component {
render() {
return (
<div>
<BrowserRouter>
</BrowserRouter>
</div>
)
}
}
- Create a new functional component to use as a 'Home' route
import React from 'react'
const Home = props =>
<div>
<h1>Welcome To the homepage</h1>
</div>
export default Home
- Import the
Routecomponent from 'react-router-dom`
import { BrowserRouter, Route } from 'react-router-dom'
- Add routes inside the
<BrowserRouter>component (make sure importHome)
<Route path="/" component={Home}/>
- Create a new functional component to use as a 'Page' route
import React from 'react'
const Page = props =>
<div>
<h1>Welcome To the page</h1>
</div>
export default Page
- Add route inside the
<BrowserRouter>component (make sure importPage)
<Route path="/" component={Page}/>
-
What error appears? How would you fix it?
-
Import
Switchfromreact-router-dom
import { BrowserRouter, Route, Switch } from 'react-router-dom'
- Add
Switchas the only child ofBrowserRouter
<BrowserRouter>
<Switch>
<Route path="/" component={Home}/>
<Route path="/page" component={Page} />
</Switch>
</BrowserRouter>
Switch will only display the the first route where the path matches the url. W
ith out Switch, it will display all the routes that match.
- Change the url to
/page. Does anything change? What do you think is happening?
When the url is getting is getting matched, it's very optimistic. It'll match if any part of the path matches.
In this case, it is matching / of /page to the / route, and because it is in a Switch it only shows the
first one that matches.
In order to prevent eager matching, there are 2 solutions
- Order routes in such a way that eager matching helps, more complex to less complex
- Add the prop
exactto each route.
- Add a Link from
/to/pageInHome.jsimportLinkfromreact-router-dom
import { Link } from 'react-router-dom'
- Add the link component to the JSX
<Link to="/page">Page</Link>
- Add a new route that contains a wildcard in the
path
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/page" component={Page} />
<Route exact path="/page/:id" component={SpecificPage} /> // <- add this
</Switch>
</BrowserRouter>
- Create a new functional component name
SpecificPage.js, make sure to import inApp.js
import React from 'react'
const SpecificPage = props => {
console.log(props)
return (
<div>
Specific page {props.match.params.id}
</div>
)
}
export default SpecificPage
- Go the the url
/page/3. What do you see?
When a component is rendered using <Route/>, it is given information about how it was routed.
Any wildcards in the url will be found in the match key of props.
- In
App.jsimport and addBrowserRouterandSwitch - Comment out all
AddPostandViewPost - Create a route for
/using componentViewPosts
- How do you pass
this.state.poststoViewPosts - Why do you need to pass props down?
- Create a route or
/newusing componentAddPost
- How do you pass
this.addPosttoAddPost
- Create a route for
/:postIdusing Component
- Create a method on
App.jsnamedthis.getPostthat receives and id, and returns the post by that id - How do you pass
this.getPosttoViewPost
- After a new post has been added, return to the main page
- Where does
historymethod come from?
- In
ViewPost, redirect back the/if thepostIddoes not match with a post.id
- In groups of 2
- One person open up the
with-routerbranch - One person open up the
with-reduxbranch
- One person open up the
- Compare and contrast the changes
- Why is data and actions not being passed down on the routes?
- What happens when a new post is added?
- Why is
history.pushin a callback that is passed to theaddPostactions?