简体   繁体   English

在 NextJS 中使用 useContext 和 useReducer 并将 null 作为上下文读取

[英]Using useContext and useReducer in NextJS and reading null as context

Attempting to create a tic-tac-toe game in NextJS and trying to create a board context for my components to read.尝试在 NextJS 中创建井字游戏,并尝试为我的组件创建棋盘上下文以供阅读。 Once I introduced a reducer into the Wrapper for a more complex state, I am now getting a null error.一旦我在 Wrapper 中引入了一个 reducer 以获得更复杂的状态,我现在就会收到一个 null 错误。 Errors: TypeError: Cannot read properties of null (reading 'useContext')错误: TypeError: Cannot read properties of null (reading 'useContext')

Error in then the terminal:然后终端出错:

Warning: Invalid hook call.警告:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在函数组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app你可能在同一个应用程序中拥有多个 React 副本

I believe I only have the react version installed in this project that next installs when using create-next-app.我相信我只在这个项目中安装了 react 版本,下次使用 create-next-app 时安装。 I'm largely stuck on where to look for what's going wrong.我在很大程度上被困在哪里寻找问题所在。 Can I not use the useReducer hook instead of the useState hook when providing context in NextJS?在 NextJS 中提供上下文时,我可以不使用 useReducer 挂钩而不是 useState 挂钩吗?

Context Declaration:上下文声明:

const TTTContext = createContext();
const TTTBoard = new Board(3);

const gameStateReducer = (state, action) => {
    switch (action.type) {
        //...reducer logic
    }
}

const TTTWrapper = ({ children }) => {    
    const [gameState, dispatch] = useReducer(gameStateReducer, {board: TTTBoard, playerPiece:"DEFAULT"});
    const gameContextValue = () => {gameState, dispatch};
    return (
        <TTTContext.Provider value={gameContextValue}>
            {children}
        </TTTContext.Provider>
    );
}

const useTTTContext = () => {
    return useContext(TTTContext)
};

module.exports = { TTTWrapper, useTTTContext }

The context is applied in _app.js here:上下文在 _app.js 中应用:

    <TTTWrapper>
      <Component {...pageProps} />
    </TTTWrapper>

And the game state is retrieved in the actual Grid component here:游戏状态在此处的实际 Grid 组件中检索:

import { useTTTContext } from "../contexts/TTTContext";
const {gameState, dispatch} = useTTTContext();

Solved: const {gameState, dispatch} = useTTTContext();已解决: const {gameState, dispatch} = useTTTContext(); was outside of its component body.位于其组件主体之外。

From what I can see of the code you've provided, the issue seems to be in the last snippet:从我所看到的您提供的代码来看,问题似乎出在最后一个片段中:

import { useTTTContext } from "../contexts/TTTContext";
const {gameState, dispatch} = useTTTContext();

The useTTTContext doesn't appear to be used/called inside any React function component body nor any custom React hook. useTTTContext似乎没有在任何 React 函数组件主体或任何自定义 React 挂钩中使用/调用。 It should be moved into either a React function or custom hook body.它应该被移动到 React 函数或自定义钩子体中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有自定义钩子的 useContext 与 useContext + useReducer - using useContext with custom hooks vs useContext + useReducer 使用 React useContext 和 useReducer 时出现打字稿错误 - Typescript error when using React useContext and useReducer 关于使用 useContext 和 useReducer 钩子的问题 - Question around using useContext and useReducer hooks typescript 中的 useContext 和 useReducer - useContext and useReducer in typescript 在 React 中使用 useContext 和 useReducer 创建新的键值对 - Create new key-value pairs using useContext and useReducer in React TypeError:无法读取 null 的属性(读取“useContext”) - TypeError: Cannot read properties of null (reading 'useContext') 如何正确使用 useReducer 和 useContext? - How to properly use useReducer with useContext? 错误:在 React useContext 和 useReducer 中未定义 - ERROR: undefined in React useContext and useReducer 使用 useReducer/useContext 和 React-Testing-Library 时出现“TypeError: dispatch is not a function” - "TypeError: dispatch is not a function" when using useReducer/useContext and React-Testing-Library 未捕获的类型错误:将 useReducer 和 useContext 用于全局存储 React js 时,对象不可迭代 - Uncaught TypeError: Object is not iterable when using useReducer with useContext for global store React js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM