Skip to content

Commit 8498b60

Browse files
committed
update project for new react and react-router-dom
1 parent a58012b commit 8498b60

File tree

7 files changed

+6526
-22039
lines changed

7 files changed

+6526
-22039
lines changed

package-lock.json

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

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@testing-library/jest-dom": "^4.2.4",
7-
"@testing-library/react": "^9.3.2",
8-
"@testing-library/user-event": "^7.1.2",
9-
"axios": "^0.19.2",
10-
"bootstrap": "^4.4.1",
11-
"react": "^16.13.0",
12-
"react-dom": "^16.13.0",
13-
"react-router-dom": "^5.1.2",
14-
"react-scripts": "3.4.0"
6+
"@testing-library/jest-dom": "^5.16.4",
7+
"@testing-library/react": "^13.0.1",
8+
"@testing-library/user-event": "^14.1.1",
9+
"axios": "^0.27.2",
10+
"bootstrap": "^4.6.2",
11+
"react": "^18.2.0",
12+
"react-dom": "^18.2.0",
13+
"react-router-dom": "^6.4.0",
14+
"react-scripts": "5.0.1"
1515
},
1616
"scripts": {
1717
"start": "react-scripts start",

src/App.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from "react";
2-
import { Switch, Route, Link } from "react-router-dom";
2+
import { Routes, Route, Link } from "react-router-dom";
33
import "bootstrap/dist/css/bootstrap.min.css";
44
import "./App.css";
55

@@ -30,11 +30,12 @@ class App extends Component {
3030
</nav>
3131

3232
<div className="container mt-3">
33-
<Switch>
34-
<Route exact path={["/", "/tutorials"]} component={TutorialsList} />
35-
<Route exact path="/add" component={AddTutorial} />
36-
<Route path="/tutorials/:id" component={Tutorial} />
37-
</Switch>
33+
<Routes>
34+
<Route path="/" element={<TutorialsList/>} />
35+
<Route path="/tutorials" element={<TutorialsList/>} />
36+
<Route path="/add" element={<AddTutorial/>} />
37+
<Route path="/tutorials/:id" element={<Tutorial/>} />
38+
</Routes>
3839
</div>
3940
</div>
4041
);

src/common/with-router.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useLocation, useNavigate, useParams } from "react-router-dom";
2+
3+
export const withRouter = (Component) => {
4+
function ComponentWithRouterProp(props) {
5+
let location = useLocation();
6+
let navigate = useNavigate();
7+
let params = useParams();
8+
return <Component {...props} router={{ location, navigate, params }} />;
9+
}
10+
return ComponentWithRouterProp;
11+
};

src/components/tutorial.component.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { Component } from "react";
22
import TutorialDataService from "../services/tutorial.service";
3+
import { withRouter } from '../common/with-router';
34

4-
export default class Tutorial extends Component {
5+
class Tutorial extends Component {
56
constructor(props) {
67
super(props);
78
this.onChangeTitle = this.onChangeTitle.bind(this);
@@ -23,7 +24,7 @@ export default class Tutorial extends Component {
2324
}
2425

2526
componentDidMount() {
26-
this.getTutorial(this.props.match.params.id);
27+
this.getTutorial(this.props.router.params.id);
2728
}
2829

2930
onChangeTitle(e) {
@@ -106,7 +107,7 @@ export default class Tutorial extends Component {
106107
TutorialDataService.delete(this.state.currentTutorial.id)
107108
.then(response => {
108109
console.log(response.data);
109-
this.props.history.push('/tutorials')
110+
this.props.router.navigate('/tutorials');
110111
})
111112
.catch(e => {
112113
console.log(e);
@@ -193,3 +194,5 @@ export default class Tutorial extends Component {
193194
);
194195
}
195196
}
197+
198+
export default withRouter(Tutorial);

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import React from "react";
2-
import ReactDOM from "react-dom";
2+
import { createRoot } from "react-dom/client";
33
import { BrowserRouter } from "react-router-dom";
44

55
import App from "./App";
66
import * as serviceWorker from "./serviceWorker";
77

8-
ReactDOM.render(
8+
const container = document.getElementById("root");
9+
const root = createRoot(container);
10+
11+
root.render(
912
<BrowserRouter>
1013
<App />
11-
</BrowserRouter>,
12-
document.getElementById("root")
14+
</BrowserRouter>
1315
);
1416

1517
serviceWorker.unregister();

0 commit comments

Comments
 (0)