将 nextjs 更新到 9.0.0 后,此问题在构建过程中发生。
nex^8.1.0 → ^9.0.0
在页面组件中,我使用的是我之前设置的全局存储。
const { state, dispatch } = React.useContext(React.createContext())
错误信息是
TypeError:无法读取未定义的属性“state”,> 发生构建错误
最佳答案
原来我必须为 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 })
关于javascript - 升级到 NextJS 9.0 后如何修复 React Hooks?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56939904/