将NextJS更新为9.0.0之后,此问题在构建过程中发生。 nex^8.1.0 → ^9.0.0
在页面组件中,我正在使用以前设置的全局商店。
const { state, dispatch } = React.useContext(React.createContext())
错误消息是
TypeError: Cannot read property 'state' of undefined, > Build error occurred
结果我必须为 React.createContext()
提供默认值我有初始值,但是我将它们传递给Store.Provider
,
export function StoreProvider(props) {
const [state, dispatch] = React.useReducer(reducer, initialState);
const value = { state, dispatch };
return <Store.Provider value={value}>{props.children}</Store.Provider>}
我只需要将initialState
添加到createContext()
export const Store = React.createContext({ state: initialState })