Skip to content

Commit fd68264

Browse files
committed
added hash router
1 parent cf11e84 commit fd68264

10 files changed

Lines changed: 160 additions & 227 deletions

File tree

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,42 @@ In React, we can use a library called `React Router Dom` that will handle what i
1414

1515
This will allow us to create a multi page application effect, with the lightning fast speed of a single page application.
1616

17+
## React Router
18+
19+
The library that we will be using to implement a routing system into our React application is called `react-router-dom`. Go ahead and install it into your project by running the terminal command
20+
21+
```bash
22+
$ npm install --save react-router-dom
23+
```
24+
25+
### Hash Router
26+
27+
We will be using a specific router from react router called `Hash Router`.
28+
29+
Now that we have `react-router-dom` installed in our project, let's go ahead and setup the router. In `index.js` we will import HashRouter.
30+
31+
> Make sure to wrap it in curly braces!
32+
33+
```js
34+
import {HashRouter} from 'react-router-dom';
35+
```
36+
37+
Now since we have it imported, we want to wrap our entire application in it, so we will wrap the `HashRouter` component around our `App` component that is being rendered inside of `reactDOM.render()`.
38+
39+
```js
40+
import React from 'react';
41+
import ReactDOM from 'react-dom';
42+
import App from './App';
43+
44+
// import HashRouter
45+
import {HashRouter} from 'react-router-dom';
46+
47+
ReactDOM.render(
48+
// Wrap App with HashRouter
49+
<HashRouter>
50+
<App />
51+
</HashRouter>,
52+
document.getElementById('root'));
53+
```
54+
55+
The goal for Hash Router is to keep our user interface in sync with what ther URL is. Meaning, that will display specific components on what the URL in our browser is.

package-lock.json

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"react": "^16.9.0",
77
"react-dom": "^16.9.0",
8+
"react-router-dom": "^5.0.1",
89
"react-scripts": "3.1.1"
910
},
1011
"scripts": {

src/App.css

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/App.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React, { Component } from 'react'
42

5-
function App() {
6-
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
23-
);
3+
class App extends Component {
4+
render() {
5+
return (
6+
<div>
7+
8+
</div>
9+
)
10+
}
2411
}
2512

26-
export default App;
13+
export default App;

src/App.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/index.css

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import './index.css';
43
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
64

7-
ReactDOM.render(<App />, document.getElementById('root'));
5+
// import HashRouter
6+
import {HashRouter} from 'react-router-dom';
87

9-
// If you want your app to work offline and load faster, you can change
10-
// unregister() to register() below. Note this comes with some pitfalls.
11-
// Learn more about service workers: https://bit.ly/CRA-PWA
12-
serviceWorker.unregister();
8+
ReactDOM.render(
9+
// Wrap App with HashRouter
10+
<HashRouter>
11+
<App />
12+
</HashRouter>,
13+
document.getElementById('root'));

src/logo.svg

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)