|
| 1 | +## React Router |
| 2 | + |
| 3 | + |
| 4 | +## Intro & Recap |
| 5 | + |
| 6 | +- Last class, we discussed the React Component LifeCycle. |
| 7 | +- This includes: |
| 8 | + - Component Instantiation |
| 9 | + - Updates to State |
| 10 | + - Updates to Props |
| 11 | + - Unmounting Components |
| 12 | +- Which leads us into tonight's topic: React Router |
| 13 | + |
| 14 | +## Agenda |
| 15 | + |
| 16 | +- Understand the purpose of React Router |
| 17 | +- When to use React Router |
| 18 | +- How to implement React Router |
| 19 | + |
| 20 | +## The Purpose of React Router |
| 21 | + |
| 22 | +Due to the fact that React is a library (not a complete framework), it does not aim to solve all an application's needs. |
| 23 | + |
| 24 | +While React does a great job at creating components and providing a system for managing state (in Single Page Applications, you still need a way of changing URL's and rendering new components (based on User Interaction) |
| 25 | + |
| 26 | +The best solution for this is the React Router Library. |
| 27 | + |
| 28 | +According to the React Router Documentation: |
| 29 | + |
| 30 | +> React Router is a powerful routing library built on top of React that helps you add new screens and flows to your application incredibly quickly, all while keeping the URL in sync with what's being displayed on the page. |
| 31 | +
|
| 32 | +In short, React Router takes care of rendering components for us. |
| 33 | + |
| 34 | +Let's dig in: |
| 35 | + |
| 36 | +### Install and Import React Router |
| 37 | + |
| 38 | +You'll need to install React Router through NPM |
| 39 | + |
| 40 | +`npm install react-router --save-dev` |
| 41 | + |
| 42 | +Next, you'll want to import the appropriate variables. |
| 43 | + |
| 44 | +```javascript |
| 45 | +import React from 'react'; |
| 46 | +import ReactDOM from 'react-dom'; |
| 47 | +import { Router, Link, Route } from 'react-router'; |
| 48 | + |
| 49 | +``` |
| 50 | + |
| 51 | +That's it for setup! |
| 52 | + |
| 53 | + |
| 54 | +### Component Configuration |
| 55 | + |
| 56 | +We're going to utilize 3 components |
| 57 | + |
| 58 | +Starting with a NavBar component |
| 59 | + |
| 60 | +```javascript |
| 61 | +class NavBar extends React.Component{ |
| 62 | + render(){ |
| 63 | + return( |
| 64 | + <div className="nav"> |
| 65 | + <Link to="/">Home</Link> |
| 66 | + <Link to="login">Login</Link> |
| 67 | + </div> |
| 68 | + ) |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | +You'll notice here that we've used the `<Link>` component. This has been pulled directly from the React-Router library. |
| 73 | + |
| 74 | +- `<Link to="">` replaces `<a href="">` |
| 75 | +- This provides a standard HTML anchor tag when the DOM renders |
| 76 | +- Using `<Link>` is **necessary** for React Router to implement the routing magic that we are looking for. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +Next, we'll create a Welcome Component, asking the user to Login |
| 81 | + |
| 82 | +```javascript |
| 83 | +class Welcome extends React.Component{ |
| 84 | + render() { |
| 85 | + return ( |
| 86 | + <div> |
| 87 | + <NavBar /> |
| 88 | + <h3> Press Login To Continue </h3> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +In the above example, we've brought our NavBar component in as a child component. |
| 96 | + |
| 97 | + |
| 98 | +Finally, we'll create a Login Component (for when the User is Logged In) |
| 99 | + |
| 100 | +```javascript |
| 101 | +class Login extends React.Component{ |
| 102 | + render() { |
| 103 | + return( |
| 104 | + <div> |
| 105 | + <NavBar/> |
| 106 | + <h1>You are now Logged In</h1> |
| 107 | + </div> |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Configure the Routes |
| 114 | + |
| 115 | +While on the surface, this appears to be difficult, it's surprisingly simple |
| 116 | + |
| 117 | +Step one is creating the Router |
| 118 | + |
| 119 | +```javascript |
| 120 | +let routes = ( |
| 121 | + <Router></Router> |
| 122 | +) |
| 123 | +``` |
| 124 | +We've assigned the `<Router>` component to a variable named `routes` |
| 125 | + |
| 126 | + |
| 127 | +Next, we'll nest `<Route>`'s as children of the `<Router>` |
| 128 | + |
| 129 | +```javascript |
| 130 | +let routes = ( |
| 131 | + <Router> |
| 132 | + <Route name="welcome" path="/" component={Welcome}/> |
| 133 | + <Route name="login" path="/login" component={Login}/> |
| 134 | + </Router> |
| 135 | +); |
| 136 | +``` |
| 137 | + |
| 138 | +### Render the Router |
| 139 | + |
| 140 | +In our ReactDOM.render method, we'll just need to pass in the `routes` variable as our first argument |
| 141 | + |
| 142 | +```javascript |
| 143 | +let mountPoint = document.getElementById("root"); |
| 144 | +ReactDOM.render(routes, mountPoint) |
| 145 | +``` |
| 146 | + |
| 147 | +This allows React Router to handle rendering the appropriate components. |
| 148 | + |
| 149 | +### History |
| 150 | + |
| 151 | +You've probably noticed that the URL bar has really random strings appended to the end of your URI. This is through a concept known as hashHistory. Essentially, the `#` is used to manage routing performed on the client side. |
| 152 | + |
| 153 | +Currently, The React Router documentation recommends using `browserHistory` to address this issue, and clean up the URL's. |
| 154 | + |
| 155 | +```javascript |
| 156 | +import { Router, Link, Route, browserHistory } from 'react-router'; |
| 157 | + |
| 158 | + ... |
| 159 | + |
| 160 | +let routes = ( |
| 161 | +<Router history={browserHistory}> |
| 162 | + ... |
| 163 | +</Router> |
| 164 | +); |
| 165 | +``` |
| 166 | + |
| 167 | +With browserHistory implemented, your `<Router>` component knows which *history tracking* stragety to use, making the URL looks significantly better. |
| 168 | + |
| 169 | + |
| 170 | +## Exercise and Homework |
| 171 | + |
| 172 | +- Work with your partner to complete the [React Router Tutorial](https://github.com/ttsJavaScriptApps/React-Router-Tutorial) |
| 173 | +- Clone the repo |
| 174 | +- Create a new branch |
| 175 | +- Push the changes back up to the class GitHub repo |
| 176 | + |
| 177 | + |
| 178 | +### Full Code Sample: |
| 179 | + |
| 180 | +```javascript |
| 181 | +import React from 'react'; |
| 182 | +import ReactDOM from 'react-dom'; |
| 183 | +import { Router, Link, Route, browserHistory } from 'react-router'; |
| 184 | + |
| 185 | +class NavBar extends React.Component{ |
| 186 | + render(){ |
| 187 | + return( |
| 188 | + <div className="nav"> |
| 189 | + <Link to="/">Home</Link> |
| 190 | + <Link to="login">Login</Link> |
| 191 | + </div> |
| 192 | + ) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +class Welcome extends React.Component{ |
| 197 | + render() { |
| 198 | + return ( |
| 199 | + <div> |
| 200 | + <NavBar /> |
| 201 | + <h3> Press Login To Continue </h3> |
| 202 | + </div> |
| 203 | + ); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | + |
| 208 | +class Login extends React.Component{ |
| 209 | + |
| 210 | + render() { |
| 211 | + return( |
| 212 | + <div> |
| 213 | + <NavBar/> |
| 214 | + <h1>You are now Logged In</h1> |
| 215 | + </div> |
| 216 | + ); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +let routes = ( |
| 222 | + <Router history={browserHistory}> |
| 223 | + <Route name="welcome" path="/" component={Welcome}/> |
| 224 | + <Route name="login" path="/login" component={Login}/> |
| 225 | + </Router> |
| 226 | +); |
| 227 | + |
| 228 | + |
| 229 | +let mountPoint = document.getElementById("root"); |
| 230 | +ReactDOM.render(routes, mountPoint) |
| 231 | +``` |
0 commit comments