简体   繁体   English

NextJS React Context API 在使用 useState() 时不更新值

[英]NextJS React Context API not updating value when using useState()

I have a React Context API in use in my NextJS web app.我在我的 NextJS Web 应用程序中使用了一个 React Context API。
I'd like to persist a value throughout the web app.我想在整个网络应用程序中保持一个值。 I know I can use the Consumer tag inside the Provider tag to update the value.我知道我可以使用Provider标签内的Consumer标签来更新值。

Like so:像这样:

<ProfileContext.Provider>
    <ProfileContext.Consumer>{... some logic ...}</ProfileContext.Consumer>
</ProfileContext.Provider>

But I'd like to use the useContext() call to update a value by passing in a State object.但我想使用useContext()调用通过传入 State 对象来更新值。

user_profile.js user_profile.js

import { useState } from 'react'
import { createContext, useContext } from 'react'

const ProfileContext = createContext()

export function ProfileProvider({ children }) {
  const [profile, setProfile] = useState("default value")

  return (
    <ProfileContext.Provider value={{ profile, setProfile }}>
      {children}
    </ProfileContext.Provider>
  )
}

export const useProfileContext = () => {
  return useContext(ProfileContext)
}

The issue is the value I assign using setProfile(...) does not retain value when I navigate to another page.问题是当我导航到另一个页面时,我使用setProfile(...)分配的值不保留值。

user_account.js user_account.js

...
export default function UserAccount() {
  const profileContext = useProfileContext()

  profileContext.setProfile("SOME TEXT PROFILE VALUE")
  console.log("PROFILE CONTEXT: ", profileContext.profile)  // This will show previously set value 

  return (...)
}

_app.js _app.js

export default function App(pageProps) {
  return (
    <ProfileProvider>
      <Component {...pageProps} />
    </ProfileProvider>
  )
}

When I navigate to some other page, the state will not have changed and will return null or the default value.当我导航到其他页面时,状态不会改变,将返回 null 或默认值。

some_other_page.js some_other_page.js

...
export default function Home() {
  const profileContext = useProfileContext()
  console.log(profileContext.profile)  // "default value" 

  return (...)
}

My main guide was https://www.netlify.com/blog/2020/12/01/using-react-context-for-state-management-in-next.js我的主要指南是https://www.netlify.com/blog/2020/12/01/using-react-context-for-state-management-in-next.js

Most other examples I've read use the Provier-Consumer tags to access and change the Context value.我读过的大多数其他示例都使用Provier-Consumer标签来访问和更改 Context 值。

If you want to persist the value through page navigation or refreshes, you'll likely need to store the profile value in something that's persisted and load the value from the data store to the context.如果您想通过页面导航或刷新来保留值,您可能需要将配置文件值存储在持久的东西中,并将值从数据存储加载到上下文中。 You can use something like a db or possibly local storage.您可以使用数据库或可能的本地存储之类的东西。 On app load, you'll make the API call to retrieve these values and store it in your context.在应用加载时,您将调用 API 来检索这些值并将其存储在您的上下文中。

You should wrap profileContext.setProfile call into useEffect您应该将profileContext.setProfile调用包装到 useEffect

  useEffect(() => {
    profileContext.setProfile("SOME TEXT PROFILE VALUE")
  }, [profileContext]);

Cannot update a component while rendering a different component warning 在呈现不同的组件警告时无法更新组件

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

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