<muithemeprovider>中的附加组件导致空白页面没有错误消息

时间:2016-07-01 04:43:22

标签: javascript meteor material-ui

我最近使用npm install --save material ui

将Material UI安装到我的Meteor应用程序中

我已在<Header />文件中显示app.js组件,但每当我添加其他组件时,localhost:3000只会显示一个空白页。请参阅下面的代码:

header.js

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

class Header extends Component {
  render() {
    return(
      <AppBar
        title="Header"
        titleStyle={{textAlign: "center"}}
        showMenuIconButton={false}
      /> 
   );
  }
}

export default Header;

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

import Header from './components/header'; 
import NewPost from './components/new_post';

const App = () => {
  return (
    <MuiThemeProvider>
      <Header />
    </MuiThemeProvider>
  );  
};

Meteor.startup(() => {
  ReactDOM.render(<App />, document.querySelector('.render-target'));
});

上面的代码工作正常(见下面的截图) Working with one component (<Header />)

但是,如果我添加其他组件,我会得到一个空白屏幕

header.js是相同的

new_post.js

import React, { Component } from 'react';
import TextField from 'material-ui/TextField';

class NewPost extends Component {
  render() {
    return (
      <TextField
      hintText="Full width"
      fullWidth={true}
      />
    );
  }
}

export default NewPost;

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

import Header from './components/header'; 
import NewPost from './components/new_post';

const App = () => {
  return (
    <MuiThemeProvider>
      <Header />
      <NewPost />
    </MuiThemeProvider>
  );  
};

Meteor.startup(() => {
  ReactDOM.render(<App />, document.querySelector('.render-target'));
});

结果只是一个空白屏幕

为什么在<NewPost />中添加一个组件(<MuiThemeProvider>)会导致空白屏幕?我提到了material-ui文档及其示例项目,但它们的应用程序结构与我的不同。有什么建议?如果您需要更多信息以使这个问题更清楚,请告诉我。

2 个答案:

答案 0 :(得分:18)

哇很奇怪,但我设法通过简单地添加<div>

来实现它

<强> app.js

const App = () => {
  return (
    <MuiThemeProvider muiTheme={getMuiTheme()}>
      <div>
        <Header />
        <NewPost />
      </div>
    </MuiThemeProvider>
  );  
}
Meteor.startup(() => {
  ReactDOM.render(<App />, document.querySelector('.render-target'));
});

working app

如果有人能解释为什么添加div使这一切全部有用,我将非常感激。谢谢!

答案 1 :(得分:0)

MuiThemeProvider呈现为null,因此您必须包装子级来做任何事情-例如React.Fragment