You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MUI Grid组件时出现Uncaught TypeError错误的技术求助

问题解决:使用MUI Grid组件时出现Cannot read properties of null (reading 'useContext')报错

核心原因

你的项目没有安装Material-UI(MUI)的核心依赖包,虽然代码里导入了@mui/material/Grid,但该包并未在package.json的依赖列表中存在,导致组件无法正常加载内部依赖的Context相关逻辑,从而抛出错误。

解决步骤

  1. 安装MUI核心依赖
    在项目根目录执行以下命令:
npm install @mui/material @emotion/react @emotion/styled

若使用yarn则执行:

yarn add @mui/material @emotion/react @emotion/styled

注:MUI v5版本需要依赖@emotion/react和@emotion/styled作为样式引擎,必须一并安装

  1. 重启开发服务器
    依赖安装完成后,重启你的React开发服务器,确保新安装的包被正确加载。

验证

重新运行项目后,Grid组件应该能正常渲染,不会再出现useContext相关的报错。

内容的提问来源于stack exchange,提问作者Mel Carlo Iguis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:01:32