【问题标题】:How to fix React Hooks after upgrade to NextJS 9.0?升级到 NextJS 9.0 后如何修复 React Hooks?
【发布时间】:2019-11-18 06:18:38
【问题描述】:
将 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
【问题讨论】:
标签:
javascript
reactjs
react-hooks
next.js
【解决方案1】:
原来我必须为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 })