2015-06-08 17 views
2

我正在使用browserify来管理我的依赖关系,如material-ui设置文档中的建议。当我尝试运行我的代码,控制台给我这个消息:无法读取未定义的'组件':使用material-ui和browserify

Uncaught TypeError: Cannot read property 'component' of undefined,这是追溯到bundle.js:6769

在我bundle.js文件,行6769是这个函数的返回语句:

getThemeButton: function getThemeButton() { 
     return this.context.muiTheme.component.button; 
    }, 

我在这里错过了什么?我要求声明材料用户界面和我正在使用的material-ui组件。

我index.js文件的开头是这样的:

var React = require('react'); 
    var injectTapEventPlugin = require('react-tap-event-plugin'); 
    var mui = require('material-ui'); 
    var RaisedButton = mui.RaisedButton; 

    injectTapEventPlugin(); 
+0

没有足够的代码来查看问题。你有没有为你的组件定义'contextTypes'? – imcg

+0

我完全错过了。现在它工作:-) – tinazheng

回答

2

发现隐藏在docs答案!

显然,这是物质的UI的必要组成部分(我不知道他们为什么不把它作为一组工作的一部分),但我需要包括在我的渲染类这个片段:

childContextTypes: { 
    muiTheme: React.PropTypes.object 
}, 

getChildContext: function() { 
    return { 
     muiTheme: ThemeManager.getCurrentTheme() 
    }; 
},