[英]Connecting two contexts return undefined values in the consumer context react hooks
I really do not understand why this is not working, basically, I have a header component with its own context.我真的不明白为什么这不起作用,基本上,我有一个带有自己上下文的标题组件。 On the other hand, I have a popOver component that goes inside the header, and this popOver also has its own context.
另一方面,我有一个放在标题内部的 popOver 组件,并且这个 popOver 也有它自己的上下文。
Now, there is a list of elements that are rendered inside the popOver, the user picks which elements to render, and such list needs to be rendered simultaneously in the header, for that reason I am trying to keep both contexts synchronized, the problem appears when I try to consume the header context inside the popOver context, the values consumed appear to be undefined.现在,有一个在 popOver 中呈现的元素列表,用户选择要呈现的元素,并且这样的列表需要在标题中同时呈现,因此我试图保持两个上下文同步,出现问题当我尝试在 popOver 上下文中使用标头上下文时,使用的值似乎是未定义的。
const HeaderContext = createContext();
export const HeaderProvider = ({ children }) => {
const [headChipList, setHeadChipList] = useState([]);
const [isChipOpen, setIsChipOpen] = useState(false);
useEffect(() => {
if (headChipList.length) setIsChipOpen(true);
}, [headChipList]);
return (
<HeaderContext.Provider value={{ headChipList, setHeadChipList, isChipOpen, setIsChipOpen }}>
{children}
</HeaderContext.Provider>
);
};
export const useHeaderContext = () => {
const context = useContext(HeaderContext);
if (!context) throw new Error('useHeaderContext must be used within a HeaderProvider');
return context;
};
As you can see at the end there's a custom hook that allows an easier consumption of the context and also is a safeguard in case the custom hook is called outside context, the popOver context follows this same pattern:正如您在最后看到的那样,有一个自定义钩子可以更轻松地使用上下文,并且在自定义钩子在上下文之外调用的情况下也是一种保障,popOver 上下文遵循相同的模式:
import React, { useState, useContext, createContext, useEffect } from 'react';
import { useHeaderContext } from '(...)/HeaderProvider';
const PopoverContext = createContext();
export const PopoverProvider = ({ children }) => {
const { setHeadChipList, headChipList } = useHeaderContext; // this guys are undefined
const [menuValue, setMenuValue] = useState('Locations with Work Phases');
const [parentId, setParentId] = useState('');
const [chipList, setChipList] = useState([]);
const [locations, setLocations] = useState([]);
useEffect(() => setChipList([...headChipList]), [headChipList]);
useEffect(() => setHeadChipList([...chipList]), [chipList, setHeadChipList]);
return (
<PopoverContext.Provider
value={{
menuValue,
setMenuValue,
chipList,
setChipList,
parentId,
setParentId,
locations,
setLocations
}}
>
{children}
</PopoverContext.Provider>
);
};
export const usePopover = () => {
const context = useContext(PopoverContext);
if (!context) throw new Error('usePopover must be used within a PopoverProvider');
return context;
};
I would really appreciate any highlight about this error, hopefully, I will be able to learn how to avoid this type of error in the future我非常感谢有关此错误的任何亮点,希望我能够学习如何在将来避免此类错误
You're not calling the useHeaderContext function.您没有调用 useHeaderContext 函数。 In PopoverProvider, change the line to
在 PopoverProvider 中,将行更改为
const { setHeadChipList, headChipList } = useHeaderContext();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.