我最近使用npm install --save material ui
我已在<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'));
});
但是,如果我添加其他组件,我会得到一个空白屏幕
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
文档及其示例项目,但它们的应用程序结构与我的不同。有什么建议?如果您需要更多信息以使这个问题更清楚,请告诉我。
答案 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'));
});
如果有人能解释为什么添加div
使这一切全部有用,我将非常感激。谢谢!
答案 1 :(得分:0)
MuiThemeProvider呈现为null,因此您必须包装子级来做任何事情-例如React.Fragment