无法从上下文API获取状态设定器,获得setGlobal不是一个函数

发布于 2025-02-09 17:23:05 字数 1038 浏览 1 评论 0原文

我对一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉墨 2025-02-16 17:23:06

确保您正在调用此行const {setGlobal} = useglobal() ins app函数,而不是外部,因为它是钩子,它应该遵循挂钩规则。类似:

const App = ()={
 //...
 const { setGlobal } = useGlobal(); // inside and at the top level of the component
 //...
}

另外, global> global 是JavaScript中的保留名称。为避免混乱和可能的错误,请使用其他名称,例如globalData。最后,确保app被包装在GlobalProvider中,如下:

<GlobalProvider >
  <App/>
</GlobalProvider>

Make sure you are calling this line const { setGlobal } = useGlobal() inside App function, not outside, as it's a hook, it should follow Rules of Hooks. Something like:

const App = ()={
 //...
 const { setGlobal } = useGlobal(); // inside and at the top level of the component
 //...
}

Also, global is a reserved name in JavaScript. To avoid confusions and possible errors, use another name, like globalData. And finally make sure App is wrapped inside GlobalProvider, like this:

<GlobalProvider >
  <App/>
</GlobalProvider>
风蛊 2025-02-16 17:23:06

usecontext/useglobal是钩子,因此您不能在React组件之外使用它们。

另外,请确保您的hadlegotoa组件在globalContextProvider

import useGlobal from "../hooks/useGlobal.js";

const HandleGotoA = () => { //Changed to uppercase to follow convention
    const { setGlobal } = useGlobal();

    const id_G = item
    const id_G1 = item1
    const id_G2 = item2
    setGlobal({ id_G, id_G1, id_G2 });
   
}

useContext/useGlobal are hooks, so you cannot use them outside of a React component.

Also, make sure your hadleGoToA component is inside the GlobalContextProvider:

import useGlobal from "../hooks/useGlobal.js";

const HandleGotoA = () => { //Changed to uppercase to follow convention
    const { setGlobal } = useGlobal();

    const id_G = item
    const id_G1 = item1
    const id_G2 = item2
    setGlobal({ id_G, id_G1, id_G2 });
   
}
仙气飘飘 2025-02-16 17:23:06

我认为这与这一行有关

const { setGlobal } = useGlobal();

:我认为应该是:

const { setGlobal } = useGlobal;

因为使用括号,您正在调用该函数,因此setGlobal是useglobal函数而不是函数本身的结果。

I think it has to do with this line

const { setGlobal } = useGlobal();

I think it should be:

const { setGlobal } = useGlobal;

Because with the parenthesis, you are calling the function, therefore setGlobal is the result of the useGlobal function instead of the function itself.

云之铃。 2025-02-16 17:23:06

首先,您不能使用挂钩外部组件或自定义钩子。 https://reactjs.org/docs/hooks/hooks/hooks-rules.html

//custom hook
const useHandleGoToA = () => {
  // this line moved inside the hook 
  const { setGlobal } = useGlobal();

  const id_G = item
  const id_G1 = item1
  const id_G2 = item2
  setGlobal({ id_G, id_G1, id_G2 });

}

// component
const ComponentA = () => {
  // this line moved inside the component. 
  const { setGlobal } = useGlobal();

  const id_G = item
  const id_G1 = item1
  const id_G2 = item2
  setGlobal({ id_G, id_G1, id_G2 });

  return null;
}

First You can't use hooks outside components or custom hooks. https://reactjs.org/docs/hooks-rules.html

//custom hook
const useHandleGoToA = () => {
  // this line moved inside the hook 
  const { setGlobal } = useGlobal();

  const id_G = item
  const id_G1 = item1
  const id_G2 = item2
  setGlobal({ id_G, id_G1, id_G2 });

}

// component
const ComponentA = () => {
  // this line moved inside the component. 
  const { setGlobal } = useGlobal();

  const id_G = item
  const id_G1 = item1
  const id_G2 = item2
  setGlobal({ id_G, id_G1, id_G2 });

  return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文