简体   繁体   English

未捕获(承诺)TypeError:无法读取未定义的属性“ renderMenu”

[英]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 使用thisrenderMenu

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.

相关问题 barba.js未被捕获的TypeError:无法读取未定义的属性“ Promise” - barba.js Uncaught TypeError: Cannot read property 'Promise' of undefined 未捕获(承诺):类型错误:无法读取 ionic 2 应用程序中未定义的属性“应用” - Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined in ionic 2 app React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性&#39;props&#39; - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ forEach” - Uncaught (in promise) TypeError: Cannot read property 'forEach' of undefined 错误 - 未捕获(承诺)类型错误:无法读取未定义的属性“数据” - Error - Uncaught (in promise) TypeError: Cannot read property 'data' of undefined Axios:未捕获(承诺)TypeError:无法读取未定义的属性“协议” - Axios: Uncaught (in promise) TypeError: Cannot read property 'protocol' of undefined 未捕获(在promise中)TypeError:无法读取promiseKey.then上未定义的属性“ length” - Uncaught (in promise) TypeError: Cannot read property 'length' of undefined at promiseKey.then 未捕获(承诺)TypeError:无法读取未定义的属性“ companies” - Uncaught (in promise) TypeError: Cannot read property 'companies' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ _id” - Uncaught (in promise) TypeError: Cannot read property '_id' of undefined Leaflet 和 Mapquest:未捕获(承诺)类型错误:无法读取未定义的属性“min” - Leaflet and Mapquest: Uncaught (in promise) TypeError: Cannot read property 'min' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM