无法从 useContext 访问 dispatch

IT技术 reactjs jsx aws-amplify context-api use-context
2021-05-28 14:57:42

我有简单的商店

import React, { createContext, useReducer } from "react";
import Reducer from "./UserReducer";

const initialState = {
    user: {},
    error: null
};

const Store = ({ children }) => {
    const [state, dispatch] = useReducer(Reducer, initialState);
    return (
        <Context.Provider value={[state, dispatch]}>
            {children}
        </Context.Provider>
    );
};

export const Context = createContext(initialState);
export default Store;

我已经用它包裹了我的应用程序

<Store>
    <ThemeProvider theme={Theme}>
        <CssBaseline />
        <Navbar />
        <Wrapper>
            <Profile />
        </Wrapper>
    </ThemeProvider>{" "}
</Store>

还有一些额外的设置,我的身份验证页面位于单独的包装器中,所以我也用商店包装了它。

这是该包装器的代码(额外删除)。

import Store from "../../../context/Store";
export default function Wrapper({ children }) {
    const classes = useStyles();
    return (
        <Store>
           //different dumb containers opening
                {children}
          //different dumb containers closing
        </Store>
    );
}

现在当我尝试访问子组件中的上下文时

import React, { useContext } from "react";
import { Context } from "../../../context/Store";
import { SET_USER } from "../../../context/UserTypes";

function SignUp(props) {
    const [state, setState] = React.useState({ ...initialState });

    const [userData, dispatch] = useContext(Context);
    console.log(userData, dispatch, "check");
// rest of component

我收到以下错误

TypeError: undefined is not a function

我试图在不解构它的情况下记录 useContext 的结果,但它只有全局状态,但没有调度功能。

Reactjs 版本 = 17.0.1

更新:dispatch 可以在 withAuthenticator HOC 之外访问,但不能在里面访问,所以它可能是放大问题。

我在放大回购上打开了问题。

无法从使用 Authenticator 包装的组件访问 useContext 的调度

1个回答

我看到一些可能是潜在问题的事情。

主要问题是您的Context. 创建上下文时,它的值是一个状态 ( createContext(initialState))。但是,当您将 a 传递value给 the 时,Context.Provider您将给它一个包含状态和调度函数 ( value={[state, dispatch]})的数组您需要对上下文中包含的值保持一致。它是状态对象还是元组?

您描述的错误似乎Context是在Provider. 它会回退初始上下文值,它只是一个状态而不是一个[state, dispatch]元组。

您需要将初始值更改为与您期望的值相匹配的值。

const Context = createContext([
  initialState, // state
  () => console.error("Cannot dispatch because Context was used outside of a provider") // dispatch
]);

但是您还需要弄清楚为什么useContext从 a 获取默认值而不是一个Provider我不确定接下来的两个问题是否会解决这个问题。

看起来该Profile组件位于两个不同的Store提供程序中。一种在所有事物之外,一种在Wrapper组件内部这些将有两个单独的状态实例。当同一个上下文有两个提供者时,React 使用内部的一个。因此,您从中分派的任何操作Profile都不会更新Store.

在您的组件中使用它之后创建Context变量你应该换个顺序。Store