Delete a comment - React js

前端 未结 1 1461
谎友^
谎友^ 2021-01-19 07:08

Actually, I\'ve been trying to add a \'Delete a comment\' functionality to my Comment Box system.

Here is my code:-

var Comment = React.createClass(         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 08:00

    The function passed to map will not automatically share a this pointer with your react class.

    To use this inside the anonymous function, call .bind(this) at the end of the function definition to yield a function with the expected this inside.

    var commentNodes = this.props.comments.map(function (comment, index) {
        ...
    });
    

    Becomes:

    var commentNodes = this.props.comments.map(function (comment, index) {
        ...
    }.bind(this));
    

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