How I can check with react-router-dom 6.0.2 v

Viewed 85

I used this code to check if the user sign in or not in react-router-dom 5.3.0v but this code doesn't work with me in 6.0.2v, what instead of?

  <Route
          exact
          path="/"
          render={() => {
              return (
                this.props.loading === true ?
                <Redirect to="/login" /> :
                <Redirect to="/home" /> 
              )
          }}
        />
1 Answers

Abstract the anonymous component into an actual React component and render as JSX. Remember that the Navigate component replaced the Redirect, and the Route components render their components on the element prop.

const AuthRedirect = ({ loading }) => (
  <Navigate to={loading ? "/login" : "/home"} replace />
);

...

<Route path="/" element={<AuthRedirect loading={this.props.loading} />} />
Related