我试图将数据绑定到嵌套菜单级别的MenuItems。我可以成功地映射数据以填充主要级别,但子菜单给我一个错误。
我的JSON看起来像这样:
class Menu extends React.Component {
constructor(props) {
super(props);
this.state = {
show: false,
open: false
}
}
render() {
const { colorOption } = this.props;
//the code inside this const will work if I insert it inside my IconMenu. But it doesn't work inside the menuItems property which should be able to hold MenuItem objects
const languageMenu = {languages.map((item, index) => (
<MenuItem
key={index}
className={classnames('menu-item', {
'bg-color-medlight': ['11', '12', '13', '14', '15', '16'].indexOf(colorOption) >= 0,
'bg-color-meddark': ['21', '22', '23', '24', '25', '26'].indexOf(colorOption) >= 0
})}
primaryText={languages[index].name}
style={{userMenuItem}}
leftIcon={
<img className="flag" src={languages[index].icon}/>
}/>
))}
return(
<div style={{zIndex:2}}>
<ul className="list-unstyled">
<li>
<IconMenu
className="user"
iconButtonElement={
<RaisedButton style={ImgIconButtonStyle}><PersonalMenuIcon/></RaisedButton>
}
onChange={this.handleChange}
anchorOrigin={{horizontal: 'right', vertical: 'bottom'}}
targetOrigin={{horizontal: 'right', vertical: 'top'}}>
<MenuItem
className={classnames('menu-item', {
'bg-color-medlight': ['11', '12', '13', '14', '15', '16'].indexOf(colorOption) >= 0,
'bg-color-meddark': ['21', '22', '23', '24', '25', '26'].indexOf(colorOption) >= 0
})}
primaryText="English"
rightIcon={<ArrowDropRight />}
style={{userMenuItem}}
leftIcon={
<img className="flag" src="./assets/images/flags/uk.png"/>
}
menuItems={{languageMenu}}
/>
</IconMenu>
</li>
</ul>
</div>
);
}
}
我将子菜单保存在const中,因此我可以在渲染中使用它。 (我相信这是我的问题所在,但不确定我需要做什么)
import React from 'react';
import IconMenu from 'material-ui/IconMenu';
import MenuItem from 'material-ui/MenuItem';
import IconButton from 'material-ui/IconButton';
import Divider from 'material-ui/Divider';
import Download from 'material-ui/svg-icons/file/file-download';
import ArrowDropRight from 'material-ui/svg-icons/navigation-arrow-drop-right';
import MoreVertIcon from 'material-ui/svg-icons/navigation/more-vert';
/**
* Example of nested menus within an IconMenu.
*/
const IconMenuExampleNested = () => (
<IconMenu
iconButtonElement={<IconButton><MoreVertIcon /></IconButton>}
anchorOrigin={{horizontal: 'left', vertical: 'top'}}
targetOrigin={{horizontal: 'left', vertical: 'top'}}
>
<MenuItem
primaryText="Copy & Paste"
rightIcon={<ArrowDropRight />}
menuItems={[
<MenuItem primaryText="Cut" />,
<MenuItem primaryText="Copy" />,
<Divider />,
<MenuItem primaryText="Paste" />,
]}
/>
<MenuItem
primaryText="Case Tools"
rightIcon={<ArrowDropRight />}
menuItems={[
<MenuItem primaryText="UPPERCASE" />,
<MenuItem primaryText="lowercase" />,
<MenuItem primaryText="CamelCase" />,
<MenuItem primaryText="Propercase" />,
]}
/>
</IconMenu>
);
export default IconMenuExampleNested;
在一个完美的世界中,常规的嵌套菜单如下所示:
107 | const { colorOption } = this.props;
108 |
109 | const languageMenu = {languages.map((item, index) => (
| ^
110 | <MenuItem
111 | key={index}
112 | className={classnames('menu-item', {
我得到的错误是
模块构建失败:SyntaxError: C:/xampp/htdocs/ScalaCMReact/src/components/Header/UserMenu.js:意外的令牌,预期,(109:33)
if
@ ./src/components/Header/index.js 23:16-37
@ ./src/routes/app/components/MainApp.js
@ ./src/routes/app/index.js
@ ./src/client.js
@ multi(webpack)-dev-server / client?http://localhost:8000 webpack / hot / dev-server webpack-dev-server / client?http://0.0.0.0:8000/ webpack / hot / only-dev-server react-hot- loader / patch ./client.js
我做错了什么?
提前致谢
答案 0 :(得分:1)
看起来不是有效的javascript
const languageMenu = {languages.map((item, index) => (
<MenuItem
key={index}
className={classnames('menu-item', {
'bg-color-medlight': ['11', '12', '13', '14', '15', '16'].indexOf(colorOption) >= 0,
'bg-color-meddark': ['21', '22', '23', '24', '25', '26'].indexOf(colorOption) >= 0
})}
primaryText={languages[index].name}
style={{userMenuItem}}
leftIcon={
<img className="flag" src={languages[index].icon}/>
}/>
))}
如果您尝试创建数组数组,则外括号应为[]
。如果它是一个具有包含MenuItem数组的属性的对象,那么
const languageMenu = {languages: languages.map((item, index) => (
<MenuItem
key={index}
className={classnames('menu-item', {
'bg-color-medlight': ['11', '12', '13', '14', '15', '16'].indexOf(colorOption) >= 0,
'bg-color-meddark': ['21', '22', '23', '24', '25', '26'].indexOf(colorOption) >= 0
})}
primaryText={languages[index].name}
style={{userMenuItem}}
leftIcon={
<img className="flag" src={languages[index].icon}/>
}/>
))}
如果要将languageMenu设置为react元素,则
const languageMenu = <div>
{languages.map((item, index) => (
<MenuItem
key={index}
className={classnames('menu-item', {
'bg-color-medlight': ['11', '12', '13', '14', '15', '16'].indexOf(colorOption) >= 0,
'bg-color-meddark': ['21', '22', '23', '24', '25', '26'].indexOf(colorOption) >= 0
})}
primaryText={languages[index].name}
style={{userMenuItem}}
leftIcon={
<img className="flag" src={languages[index].icon}/>
}/>
))}
</div>