更新到 material-ui@0.15.0-beta.1 后,我无法使用 material-ui 组件

I cannot use material-ui components after update to material-ui@0.15.0-beta.1

我在控制台中收到此消息:

Failed Context Types: Required context muiTheme was not specified in AppBar

AppBar.js:158 Uncaught TypeError: Cannot read property 'prepareStyles' of undefined

我的组件中只有一个 AppBar 我认为它应该工作但是...... 这是我非常简单的代码:

import React from 'react';
import {AppBar} from 'material-ui';


    export class MyComponent extends React.Component {

        render() {
            return (
                <div>
                    <AppBar
                        title="Title"
                    />

                </div>
            );
        }

    }

感谢您的帮助...

material-ui@0.15.0.beta-1 改变了一些东西。

您可以查看下面的 link 了解更多详情。 https://github.com/callemall/material-ui/blob/master/CHANGELOG.md

因此,通过这些更改,您的代码将变为:

    import React from 'react';
    import AppBar from 'material-ui/AppBar';
    import baseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
    import getMuiTheme from 'material-ui/styles/getMuiTheme';

        export class MyComponent extends React.Component {

            getChildContext() {
                return { muiTheme: getMuiTheme(baseTheme) };
            }

            render() {
                return (
                    <div>
                        <AppBar
                            title="Title"
                        />

                    </div>
                );
            }        
        }

        MyComponent.childContextTypes = {
            muiTheme: React.PropTypes.object.isRequired,
        };

现在在 0.15.0 中你可以使用 muiThemeProvider:

...

import getMuiTheme from 'material-ui/styles/getMuiTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import MyAwesomeReactComponent from './MyAwesomeReactComponent';

const App = () => (
 <MuiThemeProvider muiTheme={getMuiTheme()}>
    <MyAwesomeReactComponent />
 </MuiThemeProvider>
)

...

因此您不必为儿童手册提供上下文。 documentation 中的更多信息。

导入MuiThemeProvider,然后用MuiThemeProvider包裹material-ui组件AppBar。

import React, { Component } from 'react';
import AppBar from 'material-ui/AppBar';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import './App.css';

class App extends Component {

  render() {

    return ( 
      <MuiThemeProvider>
        <div>
          <AppBar title = "Title" />
        </div> 
      </MuiThemeProvider>
    );
  }
}

export default App;