|
| 1 | +# 12 React Router |
| 2 | + |
| 3 | +n this sample we will start using React-Router (<acronym title="Single Page Application">SPA</acronym> navigation). |
| 4 | + |
| 5 | +We take as a starting point the example _03 State_: |
| 6 | + |
| 7 | +## Steps |
| 8 | + |
| 9 | +- Copy the content from _03 State_ and execute `npm install`. |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install |
| 13 | +``` |
| 14 | + |
| 15 | +- Let's make some cleanup (remove _src/hello.tsx_ and _src/nameEdit.tsx_ files). |
| 16 | + |
| 17 | +- Let's create a component called _PageA_ as _src/pageA.tsx_: |
| 18 | + |
| 19 | +_./src/pages/pageA.tsx_ |
| 20 | + |
| 21 | +```jsx |
| 22 | +import * as React from "react" |
| 23 | + |
| 24 | +export const PageA = () => |
| 25 | + <div> |
| 26 | + <h2>Hello from page A</h2> |
| 27 | + </div> |
| 28 | +``` |
| 29 | + |
| 30 | +- Let's create a component called _PageB_ as _src/pageB.tsx_: |
| 31 | + |
| 32 | +_./src/pages/pageB.tsx_ |
| 33 | + |
| 34 | +```jsx |
| 35 | +import * as React from "react" |
| 36 | + |
| 37 | +export const PageB = () => |
| 38 | + <div> |
| 39 | + <h2>Hello from page B</h2> |
| 40 | + </div> |
| 41 | +``` |
| 42 | + |
| 43 | +- Let's install the dependencies [`react-router-dom`](https://github.com/ReactTraining/react-router) and typescript definitions for this. |
| 44 | + |
| 45 | +```bash |
| 46 | +npm install react-router-dom --save |
| 47 | +npm install @types/react-router-dom --save-dev |
| 48 | +``` |
| 49 | + |
| 50 | +- Let's define the routing in _app.tsx_: |
| 51 | + |
| 52 | +_./src/app.tsx_ |
| 53 | + |
| 54 | +```diff |
| 55 | +import * as React from 'react'; |
| 56 | +import * as ReactDOM from 'react-dom'; |
| 57 | +- import { App } from './app'; |
| 58 | +- import { HelloComponent } from './hello'; |
| 59 | ++ import { HashRouter, Switch, Route } from 'react-router-dom'; |
| 60 | ++ import { PageA } from './pages/pageA'; |
| 61 | ++ import { PageB } from './pages/pageB'; |
| 62 | + |
| 63 | +ReactDOM.render( |
| 64 | +- <HelloComponent userName={name} /> |
| 65 | +- <NameEditComponent userName={name} onChange={setUsernameState} /> |
| 66 | ++ <HashRouter> |
| 67 | ++ <Switch> |
| 68 | ++ <Route exact={true} path="/" component={PageA} /> |
| 69 | ++ <Route path="/pageB" component={PageB} /> |
| 70 | ++ </Switch> |
| 71 | ++ </HashRouter>, |
| 72 | +document.getElementById('root') |
| 73 | +); |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +- It's time to check that we are following the right track: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm start |
| 81 | +``` |
| 82 | + |
| 83 | +- Let's define a navigation from _[PageA.tsx](./src/pageA.tsx)_ to _[PageB.tsx](./src/pageB.tsx)_. |
| 84 | + |
| 85 | +_./src/pages/pageA.tsx_ |
| 86 | + |
| 87 | +```diff |
| 88 | +import * as React from "react" |
| 89 | ++ import { Link } from 'react-router-dom'; |
| 90 | + |
| 91 | +export const PageA = () => |
| 92 | + <div> |
| 93 | + <h2>Hello from page A</h2> |
| 94 | ++ <br /> |
| 95 | ++ <Link to="/pageB">Navigate to Page B</Link> |
| 96 | + </div> |
| 97 | +``` |
| 98 | + |
| 99 | +- Let's define a navigation from _[PageB.tsx](./src/pageB.tsx)_ to _[PageA.tsx](./src/pageA.tsx)_ |
| 100 | + |
| 101 | +_./src/pages/pageB.tsx_ |
| 102 | + |
| 103 | +```diff |
| 104 | +import * as React from "react" |
| 105 | ++ import { Link } from 'react-router-dom'; |
| 106 | + |
| 107 | +export const PageB = () => |
| 108 | + <div> |
| 109 | + <h2>Hello from page B</h2> |
| 110 | ++ <br /> |
| 111 | ++ <Link to="/">Navigate to Page A</Link> |
| 112 | + </div> |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +- Let's run the app and check that the navigation links are working |
| 117 | + |
| 118 | +```bash |
| 119 | +npm start |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +# About Basefactor + Lemoncode |
| 124 | + |
| 125 | +We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. |
| 126 | + |
| 127 | +[Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. |
| 128 | + |
| 129 | +[Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. |
| 130 | + |
| 131 | +For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend |
| 132 | + |
0 commit comments