ReactJS map function cannot find property of undefined

后端 未结 3 443
情书的邮戳
情书的邮戳 2021-01-16 07:10

I\'m still learning ReactJS. I\'m challenging myself to write a very basic todo app (as one does) and I\'m having an issue calling an onClick function.

var L         


        
3条回答
  •  青春惊慌失措
    2021-01-16 07:58

    The problem you're running into is that your call to list.map will invoke the passed function with a different this than you have in your render method.

    An easy fix is to grab this in the outer scope and stash it in a variable, then use that variable in your inline function.

    render: function () {
        var self = this;
     // ^^^^^^^^^^^^^^^^
    
        var list = this.props.items;
        var items = list.map(function(item){
          return (
            
  • ); }); return (
      {items}
    ) }

提交回复
热议问题