gpt4 book ai didi

javascript - 删除评论 - React js

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:01 26 4
gpt4 key购买 nike

实际上,我一直在尝试向我的评论框系统添加“删除评论”功能。

这是我的代码:-

var Comment = React.createClass({
handleClick: function(e){
e.preventDefault();
var commentId = this.props.comment.id;
return this.props.onDelete(commentId);
},
render: function () {
return (
<div className="comment">
<h4 className="commentAuthor">
{this.props.comment.email}
</h4>
{this.props.comment.comment}
<a onClick={this.handleClick}> &times; </a>
</div>
);
}
});

var CommentList = React.createClass({
handleDelete: function(commentId){
return this.props.del(commentId);
},
render: function () {
var commentNodes = this.props.comments.map(function (comment, index) {
return (
<Comment comment = {comment} onDelete = {this.handleDelete} key = {index} />
);
});

return (
<div className="commentList">
{commentNodes}
</div>
);
}
});

var CommentBox = React.createClass({
getInitialState: function () {
return {comments: []};
},
componentDidMount: function () {
this.loadCommentsFromServer();
},
loadCommentsFromServer: function () {
$.ajax({
url: this.props.url,
dataType: 'json',
success: function (comments) {
this.setState({comments: comments});
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCommentSubmit: function(comment) {
var comments = this.state.comments;
var newComments = comments.concat([comment]);
this.setState({comments: newComments});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: {"comment": comment},
success: function(data) {
this.loadCommentsFromServer();
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
delc: function(commentId){
$.ajax({
url: this.props.url,
data: {"id" : commentId},
type: 'DELETE',
dataType: 'json',
success: function (comments) {
this.setState({comments: comments});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
render: function () {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList del={this.delc} comments={this.state.comments} />
<CommentForm onCommentSubmit={this.handleCommentSubmit}/>
</div>
);
}
});

var CommentForm = React.createClass({
handleSubmit: function() {
var email = this.refs.email.getDOMNode().value.trim();
var comment = this.refs.comment.getDOMNode().value.trim();
this.props.onCommentSubmit({email: email, comment: comment});
this.refs.email.getDOMNode().value = '';
this.refs.comment.getDOMNode().value = '';
return false;
},

render: function() {
return (
<form className="commentForm" onSubmit={this.handleSubmit}>
<input type="email" placeholder="Your email" ref="email" />
<input type="text" placeholder="Say something..." ref="comment" />
<input type="submit" value="Post" />
</form>
);
}
});

var ready = function () {
React.renderComponent(
<CommentBox url="18/comments.json" />,
document.getElementById('art')
);
};

$(document).ready(ready);

现在,当我尝试删除评论时,它会抛出一个错误,提示“Uncaught TypeError: undefined is not a function”

切换到 devTool 上的“源”选项卡,我发现问题出在“onDelete”函数中。

它说,'onDelete' 函数未定义。

我认为问题出在“this”关键字上,但我不确定

我应该怎么做才能解决这个问题?如果我错过了什么,请告诉我。(我是新手)

提前谢谢你。

最佳答案

传递给 map 的函数不会自动与你的 React 类共享一个 this 指针。

要在匿名函数中使用 this,在函数定义的末尾调用 .bind(this) 以生成具有预期 this< 的函数里面。

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

变成:

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

关于javascript - 删除评论 - React js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com