无法从上下文API获取状态设定器,获得setGlobal不是一个函数
我对一个React应用程序有问题。我需要将一系列物品保持在我的全球状态。
这是我的钩子:
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;
在我的app.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"
我找不到或无法理解原因,任何帮助都将不胜感激。
I have an issue with a React app. I need to keep an array of items in my global state.
This is my hook :
import { useContext } from "react";
import GlobalContext from "../context/GlobalProvider";
const useGlobal = () => {
return useContext(GlobalContext);
}
export default useGlobal;
And this is my provider :
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;
In my App.js I have:
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 });
}
I get a this error:
"setGlobal is not a function"
And I can't find nor understand the reason why, any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您正在调用此行
const {setGlobal} = useglobal()
insapp
函数,而不是外部,因为它是钩子,它应该遵循挂钩规则。类似:另外, global> global 是JavaScript中的保留名称。为避免混乱和可能的错误,请使用其他名称,例如
globalData
。最后,确保app
被包装在GlobalProvider
中,如下:Make sure you are calling this line
const { setGlobal } = useGlobal()
insideApp
function, not outside, as it's a hook, it should follow Rules of Hooks. Something like:Also, global is a reserved name in JavaScript. To avoid confusions and possible errors, use another name, like
globalData
. And finally make sureApp
is wrapped insideGlobalProvider
, like this:usecontext/useglobal
是钩子,因此您不能在React组件
之外使用它们。另外,请确保您的
hadlegotoa
组件在globalContextProvider
:useContext/useGlobal
are hooks, so you cannot use them outside of aReact component
.Also, make sure your
hadleGoToA
component is inside theGlobalContextProvider
:我认为这与这一行有关
:我认为应该是:
因为使用括号,您正在调用该函数,因此setGlobal是useglobal函数而不是函数本身的结果。
I think it has to do with this line
I think it should be:
Because with the parenthesis, you are calling the function, therefore setGlobal is the result of the useGlobal function instead of the function itself.
首先,您不能使用挂钩外部组件或自定义钩子。 https://reactjs.org/docs/hooks/hooks/hooks-rules.html
First You can't use hooks outside components or custom hooks. https://reactjs.org/docs/hooks-rules.html