Closed
Description
I'm tinkering with the v6 Alpha release and am running into the following console warning (see facebook/react#18178 for more information):
index.js:1 Warning: Cannot update a component from inside the function body of a different component.
in Navigate (at Authenticated.js:27)
in Unknown (at App.js:26)
in Route (at App.js:26)
in Routes (at App.js:23)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:22)
in ErrorBoundary (at App.js:21)
in App (at src/index.js:59)
in PersistGate (at src/index.js:58)
in Provider (at src/index.js:57)
In v5, I had the following component:
import React from 'react';
import { Redirect } from 'react-router-dom';
export default ({ children }) => {
const isAuthenticated = useSelector(state => !!state.auth.userId);
if (!isAuthenticated) {
return <Redirect to="/auth/login" />;
}
return children;
};
Which is implemented via:
<Switch>
<Route exact component={AuthLogin} path="/auth/login" />
<Authenticated>
<Route exact component={Dashboard} path="/" />
</Authenticated>
</Switch>
In v6, I have modified that component to be:
import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
export default () => {
const isAuthenticated = useSelector(state => !!state.auth.userId);
if (!isAuthenticated) {
return <Navigate to="/auth/login" />;
}
return <Outlet />;
};
Which is implemented via:
<Routes>
<Route exact element={<AuthLogin />} path="/auth/login" />
<Route element={<Authenticated />} path="/">
<Route exact element={<Dashboard />} path="/" />
</Route>
</Routes>
When navigating to http://localhost:3000, I am properly redirected to http://localhost:3000/auth/login, but receive the aforementioned console warning.
Is there a different way I should be doing this with v6?
Metadata
Metadata
Assignees
Labels
No labels