【发布时间】:2018-01-10 00:41:19
【问题描述】:
我有几个不同的 const 函数想要返回和渲染。我将其设置为始终呈现主菜单,除非按下按钮,这会将我的状态更新为按钮的名称。每个按钮名称都有另一组应该渲染出来的按钮(就像主菜单一样),但我无法让它们渲染出来。
我的当前代码呈现主菜单,选择按钮而不是渲染新行按钮时,它只是只能选择所选按钮的名称。如果可能的话,我希望我的条件渲染出状态名称组件。
this.state.selected 只是渲染出一个字符串。我想把它装进去……
<this.state.selected />
但是,这不起作用。
return (
<div>
{this.state.selected === '' ? <MainMenu /> : this.state.selected }
</div>
)
编辑 - 添加所有代码以获取更多信息
import React, { Component } from 'react'
import SearchBar from 'material-ui-search-bar'
import sass from '../scss/application.scss'
import PropTypes from 'prop-types'
import RaisedButton from 'material-ui/RaisedButton'
class OverviewRender extends Component {
constructor(props) {
super(props);
this.state = {
selected: ''
}
}
handleClick(name) {
this.setState({
selected: name
});
console.log("testing" + this.state.selected);
}
render() {
const MainMenu = () => (
<div>
<RaisedButton
label="File Options"
onClick={this.handleClick.bind(this, 'FileOptions')}
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
<RaisedButton
label="Setup Options"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
<RaisedButton
label="More Options"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
<RaisedButton
label="Shift Setup"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/> <br />
<RaisedButton
label="View/Report"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
<RaisedButton
label="Troubleshooting"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
</div>
);
const FileOptions = () => (
<div>
<RaisedButton
label="Load Values"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
<RaisedButton
label="Open Values From Disk"
style={{
height: '80px',
width: '180px',
fontSize: '44px',
padding: '1px'
}}
/>
</div>
);
const { Selected } = this.state;
return (
<div>
{Selected === '' ? <MainMenu /> : <Selected />}
</div>
)
}
}
class Overview extends Component {
constructor(props){
super(props)
this.state = {
open: false
}
}
render() {
return (
<div className="menuButtons">
<br />
<OverviewRender />
</div>
)
}
}
export default Overview;
【问题讨论】:
标签: reactjs components rendering