Skip to content

Commit 377655d

Browse files
committed
upd react-dom v6 for react 18
1 parent 92519e3 commit 377655d

File tree

5 files changed

+68
-139
lines changed

5 files changed

+68
-139
lines changed

package-lock.json

Lines changed: 32 additions & 73 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"react-error-overlay": "^6.0.9",
4848
"react-google-recaptcha": "^2.1.0",
4949
"react-iframe": "^1.8.0",
50-
"react-router-dom": "^5.2.0",
50+
"react-router-dom": "^6.30.1",
5151
"react-scripts": "5.0.1",
5252
"react-transition-group": "^4.4.2",
5353
"universal-cookie": "^4.0.4"
Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import React from 'react';
2-
import { Route, Redirect } from 'react-router-dom';
2+
import { Navigate, useLocation } from 'react-router-dom';
33
import { membershipState } from '../../Enums';
44
import { allowedIf } from '../../Routes';
55
import { useUser } from '../../Components/context/UserContext';
66
import { useAuth } from '../../Components/context/AuthContext';
77

8-
export default function PrivateRoute({
9-
component: Component,
10-
appProps,
11-
...params
12-
}) {
8+
export default function PrivateRoute({ element: Component, appProps, ...params }) {
139
const { user } = useUser();
1410
const { authenticated } = useAuth();
11+
const location = useLocation();
1512

16-
// Check if the user's access level matches with route's access grant
1713
const PERMISSION_LOOKUP_TABLE = {
1814
[allowedIf.MEMBER]: user?.accessLevel >= membershipState.MEMBER,
1915
[allowedIf.OFFICER_OR_ADMIN]: user?.accessLevel >= membershipState.OFFICER,
@@ -23,36 +19,11 @@ export default function PrivateRoute({
2319

2420
const isAllowed = PERMISSION_LOOKUP_TABLE[appProps.allowed] ?? false;
2521

26-
return (
27-
<Route
28-
{...params}
29-
render={(props) => {
30-
if (isAllowed) {
31-
return <Component {...appProps} {...props} />;
32-
} else if (authenticated) {
33-
return (
34-
<Redirect
35-
to={{
36-
pathname: '/',
37-
}}
38-
/>
39-
);
40-
} else {
41-
return (
42-
<Route
43-
render={(props) => (
44-
<Redirect
45-
to={{
46-
pathname:
47-
'/login?redirect=' + encodeURIComponent(params.location.pathname),
48-
state: { from: props.location },
49-
}}
50-
/>
51-
)}
52-
/>
53-
);
54-
}
55-
}}
56-
/>
57-
);
22+
if (isAllowed) {
23+
return React.cloneElement(Component, { ...appProps });
24+
} else if (authenticated) {
25+
return <Navigate to="/" replace />;
26+
} else {
27+
return <Navigate to={`/login?redirect=${encodeURIComponent(location.pathname)}`} state={{ from: location }} replace />;
28+
}
5829
}

src/Pages/Messaging/Messaging.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { useState, useEffect } from 'react';
22
import { sendMessage, connectToRoom } from '../../APIFunctions/Messaging';
3-
import { useParams, useHistory } from 'react-router-dom/cjs/react-router-dom.min';
3+
import { useParams, useNavigate } from 'react-router-dom';
44
import { useUser } from '../../Components/context/UserContext';
55

66
export default function Messaging() {
77
const { id } = useParams();
8-
const history = useHistory();
8+
const history = useNavigate();
99
const { user } = useUser();
1010
const [roomIdInput, setRoomIdInput] = useState(id || '');
1111
const [roomIdSubmit, setRoomIdSubmit] = useState(id || 'general');

src/Routing.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
2+
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
33

44
import PrivateRoute from './Components/Routing/PrivateRoute';
55
import NavBarWrapper from './Components/Navbar/NavBarWrapper';
@@ -19,7 +19,7 @@ export default function Routing({ appProps }) {
1919

2020
return (
2121
<div>
22-
<Switch>
22+
<Routes>
2323
{signedInRoutes.map(
2424
({
2525
path,
@@ -40,34 +40,33 @@ export default function Routing({ appProps }) {
4040
/>);
4141
}
4242
return (
43-
<PrivateRoute
43+
<Route
4444
key={index}
45-
exact
4645
path={path}
47-
appProps={{
48-
allowed: allowedIf,
49-
redirect,
50-
...appProps
51-
}}
52-
component={props => getCorrectComponent(props)}
46+
element={
47+
<PrivateRoute
48+
appProps={{
49+
allowed: allowedIf,
50+
redirect,
51+
...appProps
52+
}}
53+
>
54+
{getCorrectComponent({})}
55+
</PrivateRoute>
56+
}
5357
/>
5458
);
5559
}
5660
)}
57-
{signedOutRoutes.map(({ path, Component }, index) => {
58-
return (
59-
<Route
60-
key={index}
61-
exact
62-
path={path}
63-
render={props => (
64-
<NavBarWrapper component={Component} {...props} {...appProps} />
65-
)}
66-
/>
67-
);
68-
})}
69-
<Route component={NotFoundPage} />
70-
</Switch>
61+
{signedOutRoutes.map(({ path, Component }, index) => (
62+
<Route
63+
key={index}
64+
path={path}
65+
element={<NavBarWrapper component={Component} {...appProps} />}
66+
/>
67+
))}
68+
<Route path="*" element={<NotFoundPage />} />
69+
</Routes>
7170
</div>
7271
);
7372
}

0 commit comments

Comments
 (0)