使用带有ecmascript6的material-ui

时间:2015-07-20 12:25:28

标签: reactjs ecmascript-6 material-ui

我发现material-ui并且我试图以下列方式添加一些组件

import React from 'react';
import mui from 'material-ui';
import injectTapEventPlugin from 'react-tap-event-plugin';
import PropertiesList from './../components/propertylist.jsx';
import Filters from './../components/filter.jsx';
import Properties from './../models/PropertiesModel.js';
import Toolbar from './../components/toolbar.jsx';
var AppBar = mui.AppBar;
var IconButton = mui.IconButton;
var NavigationClose = mui.NavigationClose;
var ThemeManager = new mui.Styles.ThemeManager();
var FlatButton = mui.FlatButton;
injectTapEventPlugin();


class TransProperties extends React.Component {

  constructor(props){
     super(props);
   }

  render() {
    return <div className="row">
        <div className="col-lg-9">
            <AppBar
              title="Title"
              iconElementLeft={<IconButton><NavigationClose /></IconButton>}
              iconElementRight={<FlatButton label="Save" />} />
            <PropertiesList url="/properties.json"/>
        </div>
        <div className="col-lg-3">
            <Filters/>
        </div>
    </div>;
  }

   handleTouchTap() {
        alert("oh hi");
    }
}

export default TransProperties

但我收到错误Uncaught TypeError: Cannot read property 'spacing' of undefined

指的是

 getStyles: function getStyles() {
    var spacing = this.context.muiTheme.spacing;...}

我错过了什么?

1 个答案:

答案 0 :(得分:3)

您可能错过了组件上的contextType,这使muiTheme可用。

您的组件应如下所示:

class ExampleComponent extends React.Component {

  getStyles() {
    ...
    let spacing = this.context.muiTheme.spacing;
    ...
  }

  render() {
    return (
      <div style={this.getStyles()} />
    );
  }
}

// Don't forget this little nugget
ExampleComponent.contextTypes = {
  muiTheme: React.PropTypes.object
}