Skip to content

[v6] Cannot update a component from inside the function body of a different component. #7199

Closed
@ellisio

Description

@ellisio

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions