简体   繁体   中英

How to make a react class component retain its state after login and change available routes based on the state currently available in the component?

I was working on a simple react app. What I aim to do is that i want to make only the user/login and /user/register URLS accessible to the user until the user logs in. So on authentication i am changing the Routes. Currently i am being routed to /projects page(the new routes are populated in the app) but on refreshing i am being redirected to /user/login. Is there a way where I can do this without saving anything in localstorage? Can anyone help me resolve this?

import './App.css';
import LoginForm from './components/LoginForm/LoginForm'
import RegistrationForm from './components/RegistarationForm/RegistarationForm';
import Project from './components/Project/Project';
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect,
} from "react-router-dom";
import React, { Component } from 'react';

class App extends Component {

  constructor(props) {
    super(props);
    this.state={
      isUserAuthenticated: false
    };
    console.log('APP constructor');
   }
  

  // componentDidUpdate(prevProps) {
  //   console.log('update');
  // }

  authenticateUser=()=>{
    this.setState({
      isUserAuthenticated: true
    });
  }

  render(){
    var routes;
    if(!this.state.isUserAuthenticated){
      routes=<React.Fragment>
              <Route exact path="/user/login" ><LoginForm authenticateUser={this.authenticateUser}/></Route>
              <Route exact path="/user/register"><RegistrationForm /></Route>
              <Route
                render={() => <Redirect to={{pathname: "/user/login"}}/>}
              />
            </React.Fragment>;
    }else{
      routes=<React.Fragment>
              <Route exact path="/projects" ><Project /></Route>
              <Route
                render={() => <Redirect to={{pathname: "/projects"}}/>}
              />
            </React.Fragment>;
    }
    return (
      <Router>
        <Switch>
          {routes}
        </Switch>
      </Router>
    );
  }
}

export default App;

I am Brazilian and therefore I speak Portuguese, but I will use the translator to try to help you.

I didn't really understand what's going on, but I still think you can do the following:

  render() {
    return (
      <Router>
        <Switch>
          {!this.state.isUserAuthenticated ? (
            <React.Fragment>
              <Route exact path="/user/login">
                <LoginForm authenticateUser={this.authenticateUser} />
              </Route>
              <Route exact path="/user/register">
                <RegistrationForm />
              </Route>
              <Route
                render={() => <Redirect to={{ pathname: "/user/login" }} />}
              />
            </React.Fragment>
          ) : (
            <React.Fragment>
              <Route exact path="/projects">
                <Project />
              </Route>
              <Route
                render={() => <Redirect to={{ pathname: "/projects" }} />}
              />
            </React.Fragment>
          )}
        </Switch>
      </Router>
    );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM