[英]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:这可能由于以下原因之一而发生:
- You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
- You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
- 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.