2016-01-24 75 views
3

我寫了下面的代碼遺漏的類型錯誤:_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我的解決方案不適合我。

回答

3

所以在發佈你的repo後,我注意到你正在使用mui 0.14,並且經過快速研究,似乎你不再需要ThemeManager的構造函數 - 你在之前的版本中做過。

只是把它定義在導入:

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

來源:Material-UI

看標題爲例子:1.使用陣營與上下文的生命週期方法

0

原來的問題,而我這個答案回答,包含:

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

這可能是因爲你發佈了兩個答案;)。自從它是正確的,我就投了你的第一個答案。如果您認爲您發佈的內容有相關性,請將其添加到上述文章中,並刪除此文章以保持清潔。 –