简体   繁体   English

如何使反应 class 组件在登录后保留其 state 并根据组件中当前可用的 state 更改可用路由?

[英]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.我的目标是,在用户登录之前,我只想让用户访问用户/登录和/用户/注册 URL。因此,在身份验证时,我正在更改路由。 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.目前我被路由到 /projects 页面(新路由填充在应用程序中)但是在刷新时我被重定向到 /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>
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在反应组件中呈现状态数组。 该数组在状态下可用,但在呈现其显示未定义时 - how to render the state array in react component. the array is available in state but while rendering its showing undefined 在 state 更改后,React 如何更新组件及其子组件? - How does React update a component and its children after a state change? 如何基于Redux状态更改生成React组件setState()? - How to make a React component setState() based on a Redux state change? 反应:如何从其父组件更改子组件(功能组件,而不是 class 组件)的 state? - react: How to change the state of a child component(function component, not class component) from its parent? Redux状态的历史记录不可用,但在React组件上可用 - history on redux state is not availb but it is available on react component React function 组件 state 在 ComponentWillUnmount 不可用 - React function component state is not available at ComponentWillUnmount 如何在反应组件中保留道具或 state? - How to retain props or state in a react component? 登录到其他组件后响应发送状态以更改 html - React send state after login to other component to change html 反应:在功能组件中更改 State 也更改功能组件的道具值,及其父 Class State - React: Changing State in functional component also Change functional component's props value, and its Parent Class State 如何在 React 中更改组件的 state? - How to change the state of a component in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM