简体   繁体   English

为什么我的反应 state 没有正确改变?

[英]Why is my react state not changing properly?

I'm trying to authenticate using firebase in react.我正在尝试使用 firebase 进行身份验证。 This is the component that tracks my authentification state.这是跟踪我的身份验证 state 的组件。

    import { useState} from "react";


    function useAuth(fbAuth) {
       const [isAuthenticated, setIsAuthenticated] = useState(false);
       const createEmailUser = (email, password) => fbAuth.createUserWithEmailAndPassword(email, password);
       const signInEmailUser  = (email, password) => fbAuth.signInWithEmailAndPassword(email, password);
       const signOut = fbAuth.signOut();

       fbAuth.onAuthStateChanged(async user=> {
          if (user) {
             await setIsAuthenticated(true);
             console.log(isAuthenticated, "should be true")
             return
          } else {
             await setIsAuthenticated(false);
             console.log(isAuthenticated, "should be false")
             return
          }

        });

       return {isAuthenticated, createEmailUser, signInEmailUser, signOut};

    }

    export default useAuth

The console logs when I click log in are我单击登录时的控制台日志是

2useAuth.js:13 false "should be true" 2useAuth.js:13 false “应该是真的”

2useAuth.js:17 false "should be false" 2useAuth.js:17 假“应该是假的”

2useAuth.js:17 true "should be false" 2useAuth.js:17 真“应该是假的”

4useAuth.js:17 false "should be false" 4useAuth.js:17 假“应该是假的”

The setIsAuthenticated function does not return a promise, so using await here doesn't really do anything. setIsAuthenticated function 不返回 promise,所以在这里使用await并没有真正做任何事情。 On top of that, the value of isAuthenticated is never modified (calling setIsAuthenticated doesn't change the value of the variable you already set at the beginning of the hook).最重要的是,永远不会修改isAuthenticated的值(调用setIsAuthenticated不会更改您在挂钩开始时已经设置的变量的值)。 Basically, doing a console.log within your onAuthStateChanged function doesn't make sense and won't do what you expect.基本上,在onAuthStateChanged function 中执行console.log没有任何意义,也不会达到您的预期。 If you want to get a better feel for what is happening, try putting a console.log at the beginning of the function, and just print the fact that you expect it to change.如果您想更好地了解正在发生的事情,请尝试将console.log放在 function 的开头,然后打印您希望它会改变的事实。 So something like this:所以是这样的:

import { useState, useEffect } from "react";

function useAuth(fbAuth) {
   const [isAuthenticated, setIsAuthenticated] = useState(false);
   const createEmailUser = (email, password) => fbAuth.createUserWithEmailAndPassword(email, password);
   const signInEmailUser  = (email, password) => fbAuth.signInWithEmailAndPassword(email, password);
   const signOut = fbAuth.signOut();

   console.log('isAuthenticated', isAuthenticated)
   //I'm assuming you only need to register the fbAuth.onAuthStateChanged
   //callback once. So We use useEffect with an empty array to have it
   //only run the first time this is rendered.
   useEffect(() => {
       fbAuth.onAuthStateChanged(async user=> {
          if (user) {
             setIsAuthenticated(true);
             console.log('isAuthenticated should be true after this')
          } else {
             setIsAuthenticated(false);
             console.log('isAuthenticated should be false after this')
          }
        });
    }, [])

    return {isAuthenticated, createEmailUser, signInEmailUser, signOut};

}

export default useAuth

You would then expect然后你会期望

//Initial render
isAuthenticated false

//Click log-in
isAuthenticated should be true after this
isAuthenticated true

//Click log-out
isAuthenticated should be false after this
isAuthenticated false

I see nothing wrong here.我觉得这里没有错。 You are the getting the value before it is changed.您是在更改之前获取值。 To access the newest state you can use the useEffect hook.要访问最新的 state,您可以使用useEffect挂钩。 Also you have not asked anything like what is your issue and what do u expect?你也没有问过你的问题是什么,你期望什么?

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

相关问题 Firebase + React-不更改我的状态 - Firebase + React - Not changing my state 为什么 state 在反应中会自行改变? - Why is the state changing by itself in react? 更改 state 时,React 中的网格大小未正确更改 - Grid Size isn't changing properly in React when changing state 为什么 React 状态没有正确更新? - Why is the React state not being updated properly? 更改我的嵌套 state React Native 时出现问题 - Problem on changing my nested state React Native React js:我的 state 在 onClick 之后没有改变 - React js : My state is not changing after onClick 尽管更改了 state,但我的 React UI 没有更新 - My React UI is not updating although changing state 为什么即使在 React JS 中更改 state 后,我的代码也没有从 API 获取更新的详细信息? - Why my code is not fetching updated details from API's even after changing state in React JS? 为什么在 React 中更改 state 时我的子组件不更新数据? - Why isn't my child component updating data when changing the state in React? "<i>Why does changing state (useState) break my p5.js code?<\/i>为什么更改状态 (useState) 会破坏我的 p5.js 代码?<\/b> <i>(React + p5.js application)<\/i> (反应 + p5.js 应用程序)<\/b>" - Why does changing state (useState) break my p5.js code? (React + p5.js application)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM