javascript - 升级到 NextJS 9.0 后如何修复 React Hooks?
问题描述
将 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 })
推荐阅读
- swift - 我怎样才能成功使用委托?按钮不适用于代表
- python - ImportError:无法导入名称'deprecated_endpoints'?
- html - 如何使用 SVG 路径从地图中获取坐标?
- java - 为什么我从 flink 得到重复的数据?
- python - 找到一个值的子集的最佳近似值
- ruby-on-rails - Rails 仅在应用程序启动时运行一次作业
- reactjs - 字段组件中的 reduxForm 验证问题(错误:超出最大更新深度)
- android - 如何让本地化字体在运行时工作?
- python - 如何使用 Python 代码在一个绘图上叠加两个多项式回归图?
- wordpress - 将主网站网址从 http 更改为 https