Uncaught TypeError: Cannot read property 'Checked' of undefined - ReactJS

前端 未结 3 793
醉话见心
醉话见心 2020-12-21 16:45

I am trying to create a simple a TODO list app in ReactJS. My basics of React are not very clear so I am stuck here.

相关标签:
3条回答
  • 2020-12-21 17:03

    you can go with Alex Solution but you can also achieve the same thing by changing only two place like

    var that=this;
    var createItem = function(item) {
          return <li key={item.id}>{item.text}<input type="checkbox" onChange={that.Checked} /></li>;
        };
        return <ul>{this.props.items.map(createItem)}</ul>;
    
    0 讨论(0)
  • 2020-12-21 17:05

    You need set this for .map (you can do that through second argument in .map)

    return <ul>{this.props.items.map(createItem, this)}</ul>;
    

    Example

    because now in createItem this refers to global (in browsers it will be window) score (or if you use strict mode this will be undefined)

    as you use babel you also can use arrow functions

    var createItem = (item) => {
      return <li key={item.id}>
        {item.text}<input type="checkbox" onChange={this.Checked} />
      </li>;
    };
    
    return <ul>{this.props.items.map(createItem)}</ul>;
    

    Example

    0 讨论(0)
  • 2020-12-21 17:12

    Make createItem a method of TodoList class. The "this" reference is not being resolved as right now its a local function of render method. On a side note, React follows camel casing for handlers. Please rename the Clicked method to onChange, onChecked or onCheckClicked.

    0 讨论(0)
提交回复
热议问题