我对React应用程序有问题.我需要在全局状态中保留一个项目array.
这是我的钩子:
import { useContext } from "react";
import GlobalContext from "../context/GlobalProvider";
const useGlobal = () => {
return useContext(GlobalContext);
}
export default useGlobal;
这是我的提供者:
import { createContext, useState } from "react";
const GlobalContext = createContext({});
export const GlobalProvider = ({ children }) => {
const [global, setGlobal] = useState({});
return (
<GlobalContext.Provider value={{ global, setGlobal }}>
{children}
</GlobalContext.Provider>
)
}
export default GlobalContext;
在我的应用程序中.js我有:
import useGlobal from "../hooks/useGlobal.js";
const { setGlobal } = useGlobal();
const handleGotoA = () => {
const id_G = item
const id_G1 = item1
const id_G2 = item2
setGlobal({ id_G, id_G1, id_G2 });
}
我得到一个错误:
"setGlobal is not a function"
我找不到也不理解为什么,任何帮助都将不胜感激.