[英]Uncaught (in promise) TypeError: Cannot read property 'renderMenu' of undefined
I want to create a dynamic menu according to the json like this: 我想根据json创建一个动态菜单,如下所示:
[
{
"teamId": "10000",
"teamName": "笑傲江湖",
"superTeamId": "",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
},
{
"teamId": "10001",
"teamName": "计算机系",
"superTeamId": "10000",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
},
{
"teamId": "10002",
"teamName": "电子系",
"superTeamId": "10000",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
},
{
"teamId": "10003",
"teamName": "中文系",
"superTeamId": "10000",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
},
{
"teamId": "10004",
"teamName": "外文系",
"superTeamId": "10000",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
},
{
"teamId": "10005",
"teamName": "化学系",
"superTeamId": "10000",
"createTime": "2017-06-25T06:07:45.000Z",
"createUserId": null
}
]
React code 反应代码
renderMenu(team){ //recursive rendering if(team.subcats && team.subcats.length){ return ( <SubMenu key={team.teamId} title = {team.teamName} > {team.subcats.map(this.renderMenu)} </SubMenu> ); }else{ return ( <Menu.Item key={team.teamId}> {team.teamName} </Menu.Item> ); } } render(){ let {teams} = this.props; teams.forEach(e => e.subcats=teams.filter(el=>el.superTeamId==e.teamId)); teams =teams.filter(e=>e.superTeamId==''); return( <Menu mode="inline" style={{ height: '100%' }} > {teams.map(this.renderMenu)} </Menu> ) }
when the code run,it show an err: 代码运行时显示错误:
Uncaught (in promise) TypeError: Cannot read property 'renderMenu' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ renderMenu”
Can any one help me ? 谁能帮我 ?
This line 这条线
{team.subcats.map(this.renderMenu)}
tries to access context in this, but your function is not binded to the "this" you want. 尝试访问this的上下文,但是您的函数未绑定到所需的“ this”。 Try changing the following function:
尝试更改以下功能:
renderMenu(team){
//recursive rendering
if(team.subcats && team.subcats.length){
return (
<SubMenu key={team.teamId} title = {team.teamName} >
{team.subcats.map(this.renderMenu)}
</SubMenu>
);
}else{
return (
<Menu.Item key={team.teamId}>
{team.teamName}
</Menu.Item>
);
}
}
to an arrow function : 到箭头功能 :
renderMenu = (team) => {
//recursive rendering
if(team.subcats && team.subcats.length){
return (
<SubMenu key={team.teamId} title = {team.teamName} >
{team.subcats.map(this.renderMenu)}
</SubMenu>
);
}else{
return (
<Menu.Item key={team.teamId}>
{team.teamName}
</Menu.Item>
);
}
}
For use this
in renderMenu
使用
this
在renderMenu
constructor(){
super();
this.renderMenu = this.renderMenu.bind(this);
}
Or arrow function, like @user340764 said. 还是箭头功能,就像@ user340764所说。
回调作为参数将错过此,您必须在使用它之前将其绑定
like@Andrew@user340764, bind renderMenu with this(context) before use it, or you can do like this like @ Andrew @ user340764,在使用它之前将renderMenu与this(context)绑定,或者可以这样做
render(){
const that = this
let {teams} = this.props;
teams.forEach(e => e.subcats=teams.filter(el=>el.superTeamId==e.teamId));
teams =teams.filter(e=>e.superTeamId=='');
return(
<Menu mode="inline" style={{ height: '100%' }} >
{teams.map(that.renderMenu)}
</Menu>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.