遗漏的类型错误:_materialUi.Styles.ThemeManager不是一个函数

问题描述:

我写了下面的代码遗漏的类型错误:_materialUi.Styles.ThemeManager不是一个函数

import React from 'react'; 
import mui from 'material-ui'; 
import injectTapEventPlugin from 'react-tap-event-plugin'; 
let ThemeManager = new mui.Styles.ThemeManager(); 
let Colors = mui.Styles.Colors; 

injectTapEventPlugin(); 

class App extends React.Component { 
    constructor(props) { 
     super(props); 

     this.state = { 
      messages : [{id: 1, text: 'Hi'}, {id: 2, text: 'Hello'}, {id: 3, text: 'World'}, {id: 4, text: 'test'}] 
     }; 
    } 

    getChildContext() { 
     return { 
      stores: this.props.stores, 
      muiTheme: ThemeManager.getCurrentTheme() 
     }; 
    } 

    componentWillMount() { 
     ThemeManager.setPalette({ 
      primary1Color: Colors.blue500 
     });  
    } 

    render() { 
     var messageNodes = this.state.messages.map((message) => { 
      return (<div key={message.id}>{message.text}</div>); 
     }); 
     return (<div>{messageNodes}</div>); 
    } 
} 

App.childContextTypes = { 
    stores: React.PropTypes.object, 
    muiTheme: React.PropTypes.object 
}; 

export default App; 

但它不断抛出错误

Uncaught TypeError: _materialUi2.default.Styles.ThemeManager is not a function 

我已搜查,并在网上搜索,很多人解决它

https://github.com/callemall/material-ui/issues/1439

但SA我的解决方案不适合我。

所以在发布你的repo后,我注意到你正在使用mui 0.14,并且经过快速研究,似乎你不再需要ThemeManager的构造函数 - 你在之前的版本中做过。

只是把它定义在导入:

import ThemeManager from 'material-ui/lib/styles/theme-manager'; 

来源:Material-UI

看标题为例子:1.使用阵营与上下文的生命周期方法

原来的问题,而我这个答案回答,包含:

const ThemeManager = new mui.Styles.ThemeManager(); 

这是你使用const而不是let。你想用let来初始化函数类。

常数:

The const declaration creates a read-only reference to a value.

来源:Mozilla Docs

我们:

The let statement declares a block scope local variable, optionally initializing it to a value.

来源:Mozilla Docs

根据你的transpiler你应该会得到一个错误,ThemeManager是只读的

+0

我改变常量让,但仍同样的问题。我的代码在github上可用'https://github.com/abhitechdojo/MovieLensReact.git' –

+0

对于谁投了票,我想要一个解释。 – rambossa

+0

这可能是因为你发布了两个答案;)。自从它是正确的,我就投了你的第一个答案。如果您认为您发布的内容有相关性,请将其添加到上述文章中,并删除此文章以保持清洁。 –