我在 next.js 应用程序中使用 context Api 和 useReducer 钩子,当使用 contextProvider 调用 useContext 时,它会返回 undifined 。
下面是我的代码 => contextPage=>
import React, { createContext, useReducer, useContext } from 'react';
import * as types from '../constants/userConstants';
// initialized global store
export const quizContext = createContext();
// reducer
const reducer = (state, action) => {
switch (action.type) {
case types.USER_LOGIN_SUCCESS:
return // user
case types.USER_LOGOUT_SUCCESS:
return // clear user
default:
return state;
}
};
const QuizContextProvider = ({ children }) => {
const [state, dispatch] …Run Code Online (Sandbox Code Playgroud)