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

React+Mobx应用报错:App的store属性类型无效,收到undefined

解决React+Mobx中store prop type无效的报错

这个报错的根源很明确——你从mobx-react里导入的PropTypes其实是undefined!因为**mobx-react并不导出PropTypes模块**,它本来就不属于Mobx的范畴,而是由独立的prop-types包提供的。

修复步骤:

  1. 先确保项目里安装了prop-types包(如果还没装的话):
    npm install prop-types
    # 或者用yarn
    yarn add prop-types
    
  2. 修改App组件的导入语句,把PropTypes的来源改成正确的包:
    原来的代码:
    import {observer, PropTypes} from 'mobx-react';
    
    改成:
    import {observer} from 'mobx-react';
    import PropTypes from 'prop-types';
    
  3. 剩下的propTypes定义部分不需要改动,现在你的PropTypes.object已经是来自合法的prop-types包的函数了。

修改后的完整App组件示例:

import React, {PureComponent} from 'react';
import {observer} from 'mobx-react';
import PropTypes from 'prop-types';
// Some other imports

const propTypes = {
  store: PropTypes.object
};

@observer
class App extends PureComponent {
  // Some lifecycle and class functions here
  render() {
    // Render implementation here
  }
}

App.propTypes = propTypes;
export default App;

补充一句:有些旧版的Mobx文档或教程可能会写从mobx-react导入PropTypes,但那是过去的用法,现在Mobx已经把这部分剥离出去了,必须使用独立的prop-types包哦。

内容的提问来源于stack exchange,提问作者Avi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:45:58