Reactjs 如何在react组件之间传递身份验证

Reactjs 如何在react组件之间传递身份验证,reactjs,Reactjs,我在react组件之间传递身份验证时遇到问题 我想要的是当用户单击按钮登录时显示true。我该怎么做?我想留在应用程序组件的注销组件,以便在每个页面显示 在此处共享我的代码:要遵循4个步骤,每个步骤都有大量的堆栈溢出主题 a) 在App.js上设置状态挂钩 b) 然后传递一个方法来更改状态 c) 通过路由器传递方法-另一个核心主题 讨论得很好的话题 d) 调用该方法 如果未实现注销,则可以执行相同的操作,并将app.js中的状态更改为false并传递该方法 const App = () =>

我在react组件之间传递身份验证时遇到问题

我想要的是当用户单击按钮登录时显示true。我该怎么做?我想留在应用程序组件的注销组件,以便在每个页面显示


在此处共享我的代码:

要遵循4个步骤,每个步骤都有大量的堆栈溢出主题

a) 在App.js上设置状态挂钩

b) 然后传递一个方法来更改状态

c) 通过路由器传递方法-另一个核心主题

讨论得很好的话题

d) 调用该方法

如果未实现注销,则可以执行相同的操作,并将app.js中的状态更改为false并传递该方法

const App = () => {
//set the state
  const [isLogged, setIsLogged] = useState(false);
  const changeLogStatus = () => {
    setIsLogged(true);
  };
  return (
    <div>
      <div className="App">
        Hi you are Logged? {isLogged ? "true" : "false"}
        <Routes changeStatus={changeLogStatus} />
      </div>
      <div>

        <Logout isLogged={isLogged} />
      </div>
    </div>
  );
};

export default App;

export default function Routes(props) {
  return (
    <Router history={history}>
      <Switch>
// see how the method is passed through the router , very important!
        <Route
          path="/"
          exact
          render={propss => (
            <Login history={history} changeFn={props.changeStatus} />
          )}
        />
        <Route path="/dashboard" exact component={Dashboard} />
        <Route path="/logout" exact component={Logout} />
      </Switch>
    </Router>
  );
}


// then call the method to change the state in the app.js
const Login = props => {
  const [auth, setAuth] = useState(null);

  async function handleSubmit(event) {
    event.preventDefault();
    setAuth(true);
    props.changeFn();   // calling the method
    props.history.push("/dashboard");
  }

  return (
    <>
      <h1>Login</h1>
      <form onSubmit={handleSubmit}>
        <button className="btn" type="submit">
          Enter
        </button>
      </form>
    </>
  );
};

export default Login;

const-App=()=>{
//设定状态
const[isLogged,setIsLogged]=useState(false);
const changeLogStatus=()=>{
setIsLogged(真);
};
返回(
您好,您已登录?{isLogged?“true”:“false”}
);
};
导出默认应用程序;
导出默认功能路由(道具){
返回(
//看看这个方法是如何通过路由器的,非常重要!
(
)}
/>
);
}
//然后调用该方法来更改app.js中的状态
const Login=props=>{
const[auth,setAuth]=useState(null);
异步函数handleSubmit(事件){
event.preventDefault();
setAuth(真);
props.changeFn();//调用该方法
props.history.push(“/dashboard”);
}
返回(
登录
进入
);
};
导出默认登录;

4个步骤,每个步骤都有大量的堆栈溢出主题

a) 在App.js上设置状态挂钩

b) 然后传递一个方法来更改状态

c) 通过路由器传递方法-另一个核心主题

讨论得很好的话题

d) 调用该方法

如果未实现注销,则可以执行相同的操作,并将app.js中的状态更改为false并传递该方法

const App = () => {
//set the state
  const [isLogged, setIsLogged] = useState(false);
  const changeLogStatus = () => {
    setIsLogged(true);
  };
  return (
    <div>
      <div className="App">
        Hi you are Logged? {isLogged ? "true" : "false"}
        <Routes changeStatus={changeLogStatus} />
      </div>
      <div>

        <Logout isLogged={isLogged} />
      </div>
    </div>
  );
};

export default App;

export default function Routes(props) {
  return (
    <Router history={history}>
      <Switch>
// see how the method is passed through the router , very important!
        <Route
          path="/"
          exact
          render={propss => (
            <Login history={history} changeFn={props.changeStatus} />
          )}
        />
        <Route path="/dashboard" exact component={Dashboard} />
        <Route path="/logout" exact component={Logout} />
      </Switch>
    </Router>
  );
}


// then call the method to change the state in the app.js
const Login = props => {
  const [auth, setAuth] = useState(null);

  async function handleSubmit(event) {
    event.preventDefault();
    setAuth(true);
    props.changeFn();   // calling the method
    props.history.push("/dashboard");
  }

  return (
    <>
      <h1>Login</h1>
      <form onSubmit={handleSubmit}>
        <button className="btn" type="submit">
          Enter
        </button>
      </form>
    </>
  );
};

export default Login;

const-App=()=>{
//设定状态
const[isLogged,setIsLogged]=useState(false);
const changeLogStatus=()=>{
setIsLogged(真);
};
返回(
您好,您已登录?{isLogged?“true”:“false”}
);
};
导出默认应用程序;
导出默认功能路由(道具){
返回(
//看看这个方法是如何通过路由器的,非常重要!
(
)}
/>
);
}
//然后调用该方法来更改app.js中的状态
const Login=props=>{
const[auth,setAuth]=useState(null);
异步函数handleSubmit(事件){
event.preventDefault();
setAuth(真);
props.changeFn();//调用该方法
props.history.push(“/dashboard”);
}
返回(
登录
进入
);
};
导出默认登录;

如果您想在react中的整个会话中存储值,有几种方法可以遵循

  • 使用
    反应全局配置
  • npm安装反应全局配置

    然后在登录组件中:

    import config from 'react-global-configuration';
    config.set({auth: 'true'})
    
    var myAuth = this.props.location.state.auth
    
    然后在任何其他组件中使用它

    config.get('auth')
    
  • 使用本地存储

    localStorage.setItem('auth','true'))
    localStorage.getItem('auth')

  • 使用反应路由器历史记录

  • 登录时

    this.props.history.push({
                pathname: '/Home',
                state: {
                  auth: 'true'
                }
              });
    
    和在家中组件:

    import config from 'react-global-configuration';
    config.set({auth: 'true'})
    
    var myAuth = this.props.location.state.auth
    

    如果要在react中的整个会话中存储值,可以遵循几种方法

  • 使用
    反应全局配置
  • npm安装反应全局配置

    然后在登录组件中:

    import config from 'react-global-configuration';
    config.set({auth: 'true'})
    
    var myAuth = this.props.location.state.auth
    
    然后在任何其他组件中使用它

    config.get('auth')
    
  • 使用本地存储

    localStorage.setItem('auth','true'))
    localStorage.getItem('auth')

  • 使用反应路由器历史记录

  • 登录时

    this.props.history.push({
                pathname: '/Home',
                state: {
                  auth: 'true'
                }
              });
    
    和在家中组件:

    import config from 'react-global-configuration';
    config.set({auth: 'true'})
    
    var myAuth = this.props.location.state.auth