简体   繁体   English

“无法读取未定义的属性'props'” React问题

[英]“Cannot read property 'props' of undefined” React Issue

I'm building an app in React based on this tutorial. 我正在基于本教程在React中构建一个应用程序 Instead of using the updated es2016, I'm using an older way, so I'm having some trouble with the challenges that come. 我使用的是较旧的方法,而不是使用更新的es2016,因此遇到了一些挑战。 I got this error in the browser: "TypeError: Cannot read property 'props' of undefined". 我在浏览器中收到此错误:“ TypeError:无法读取未定义的属性'props'”。 I assume it's pointing to the {this.props.onDelete} part. 我认为它指向{this.props.onDelete}部分。 Here's a snippet of my code for the Notes.jsx component: 这是我的Notes.jsx组件代码片段:

var Notes = React.createClass({
render: function () {

return (
  <ul>
    {this.props.notes.map(
      function(note) {
        return (
          <li key={note.id}>
            <Note
              onTheDelete={this.props.onDelete}
              task={note.task} />
          </li>
        );
      }
    )}
  </ul>
);
}
});

And here's a snippet from App.jsx, it's parent: 这是App.jsx的代码段,它是父代码:

var App = React.createClass({
  getInitialState: function () {
    return {
      notes: [
        {
          id: uuid.v4(),
          task: 'Learn React'
        },
        {
          id: uuid.v4(),
          task: 'Do laundry'
        }
      ]
    }
  },

  newNote: function () {
    this.setState({
      notes: this.state.notes.concat([{
        id: uuid.v4(),
        task: 'New task'
      }])
    });
  },

  deleteNote: function() {
    return 'hi';
  },

  render: function () {
    var {notes} = this.state;

    return (
      <div>
        <button onClick={this.newNote}>+</button>
        <Notes notes={notes} onDelete={this.deleteNote}/>
      </div>
    );
  }
});

I deleted the actually useful parts from deleteNote to make sure no issues were there. 我从deleteNote中删除了实际上有用的部分,以确保没有问题。 I'm having a difficult time wrapping my head around using "this" and what the binding is doing in the tutorial I mentioned. 我在使用“ this”以及我在所提到的教程中进行的绑定工作时花了很多时间。

this inside the map function isn't the same as this outside of it because of how JS works. this里面map功能是不一样的this ,因为JS是如何工作之外的它。

You can save off this.props.onDelete and use it w/o the props reference: 您可以保存this.props.onDelete并在不使用props引用的情况下使用它:

render: function () {
  var onDelete = this.props.onDelete;

  return (
    <ul>
      {this.props.notes.map(
        function(note) {
          return (
            <li key={note.id}>
              <Note
                onTheDelete={onDelete}
                task={note.task} 
              />
            </li>
          );
        }
      )}
    </ul>
  );
}

Unrelated, but I'd move that map function into its own function and avoid the deep nesting. 无关,但我会将那个map函数移到它自己的函数中,避免深层嵌套。

Dave Newton's answer is entirely correct, but I just wanted to add that if you use ES6 arrow functions then you can avoid having to keep an additional reference to this, as well as removing the return statement and taking advantage of the implicit return syntax. Dave Newton的答案是完全正确的,但是我只是想补充一点,如果您使用ES6箭头函数,则可以避免必须保留对此的附加引用,以及避免删除return语句并利用隐式return语法。

var Notes = React.createClass({
    render: function () {
        return (
          <ul>
            {this.props.notes.map(
              note => {(
                  <li key={note.id}>
                    <Note
                      onTheDelete={this.props.onDelete}
                      task={note.task} />
                  </li>
                )}
            )}
          </ul>
        );
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM