简体   繁体   English

反应全局上下文没有更新

[英]react global context is not updating

I am relatively new to react and cant seem to get my global context to update.I have wrapped all my child components with the provider but it seems like my update methods in my useState() variables are not working as expected.我对反应还比较陌生,似乎无法让我的全局上下文更新。我已经用提供者包装了我所有的子组件,但我的 useState() 变量中的更新方法似乎没有按预期工作。 In my index.js file, I wrap my App component in the provider first:在我的 index.js 文件中,我首先将我的 App 组件包装在提供程序中:

import App from './App';
import reportWebVitals from './reportWebVitals';
import {
  BrowserRouter,
  Route,
  Routes
} from "react-router-dom";
import {UserContextProvider} from './components/UserContext';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(

<UserContextProvider>
  <App/>
</UserContextProvider>
);

Here is my UserContext.js file:这是我的 UserContext.js 文件:

import React from "react";
import { useState, createContext } from "react";

export const UserContext = createContext({})
export const UserContextProvider = ({children}) =>{
const [contextUsername, setUserName] = useState(null)
const [contextFirstname, setFirstName] = useState(null)
const [contextLastname, setLastName] = useState(null)
const [contextLoggedin, setLoggedIn] = useState(false)

const value={contextUsername, setUserName, contextFirstname, setFirstName, 
contextLastname,setLastName,contextLoggedin,setLoggedIn}

return(
    <UserContext.Provider value={value}>
        {children}
    </UserContext.Provider>
)

}

I am trying to access my context in my Landing component:我正在尝试在我的登陆组件中访问我的上下文:

import React, { useContext } from 'react';
import { useState } from 'react';
import {Link, useNavigate} from "react-router-dom"
import { UserContext } from './UserContext';
const Landing = () =>{
    const {contextUsername,setUserName} = useContext(UserContext)

    const onExit = (convertedData) =>{
            setUserName("test")
            console.log(contextUsername)
            navigate('/');
     }
    return(
        <div> 
           <button onLick={onExit}></button>
        </div>
    )
    }
    export default Landing

However, in my console.log statement my contextUsername is 'undefined'.但是,在我的 console.log 语句中,我的 contextUsername 是“未定义的”。

The update state doesn't work like that.更新 state 不是那样工作的。 react create a queue for state update so we can not get the latest value right after the update call.反应为 state 更新创建一个队列,因此我们无法在更新调用后立即获取最新值。 since you are console.log(contextUsername) right after setUserName it console's undefined.因为你是 console.log(contextUsername) 就在 setUserName 之后,它的控制台是未定义的。

and don't pass updateState-method(ex. setUserName) directly in context create method to change state value from there call updateState-method(ex. setUserName) like并且不要直接在上下文创建方法中传递 updateState-method(ex.setUserName) 以从那里调用 updateState-method(ex.setUserName) 更改 state 值

const UpdateUserName = (value) =>{
  setUserName(value)
}

if you want to check the updated value of contextUsername use useEffect如果你想检查 contextUsername 使用 useEffect 的更新值

  useEffect(() => {
     console.log(contextUsername);
  },[contextUsername])

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

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