未捕获的类型错误:无法读取属性'作者'在尝试将编辑功能添加到注释框时,为undefined
Uncaught TypeError: Cannot read property 'author' of undefined when trying to add edit functionality to a comment box
这是我的React组件。我遗漏了一些(不重要的)部分。我正在尝试为注释添加编辑功能,但遇到了一个错误(在底部)。
export default class CommentBox extends React.Component {
constructor() {
super()
this.state ={
showComments: false,
comments: [
{ id: uuid.v4(), author: 'Clu', body: 'Just say no to love!', avatarUrl: 'images/default-avatar.png' },
{ id: uuid.v4(), author: 'Anne Droid', body: 'I wanna know what love is...', avatarUrl: 'images/default-avatar.png' }
]
}
}
render() {
const comments = this._getComments() || [];
let commentList;
if (this.state.showComments) {
commentList = <div className="comment-list">{comments}</div>
}
return(
<div className="comment-box">
<h3>COMMENTS</h3>
{this._getPopularMessage(comments.length)}
<h4 className="comment-count">{this._getCommentsTitle(comments.length)}</h4>
<button className="comment-toggle" onClick={this._toggleShowComments.bind(this)}>{this._toggleCommentButton()}</button>
<CommentForm addComment={this._addComment.bind(this)}/>
{commentList}
</div>
);
}
_toggleShowComments(event) {
event.preventDefault()
this.setState({showComments: !this.state.showComments})
}
_addComment(author, body) {
const comment = {
id: uuid.v4(),
author: author,
body: body
}
this.setState({comments: this.state.comments.concat([comment])})
}
_getComments() {
return this.state.comments.map((comment) => {
return (<Comment
author={comment.author}
avatarUrl={comment.avatarUrl}
value ={comment.body}
editing={comment.editing}
onEditClick={this._activateEdit.bind(this, null, comment.id)}
key={comment.id}
onDelete = {this._deleteComment.bind(this, null, comment.id)}
onEdit={this._editComment.bind(this, null, comment.id)}/>)
})
}
_activateEdit(event, key) {
this.setState({comments: this.state.comments.map((comment) => {
if (comment.id === key) {
comment.editing = true
}
return comment
})})
}
我认为这就是错误所在。如果省略这一部分,代码将按预期运行。但是,当我在中添加此项时,会出现Uncaught TypeError: Cannot read property 'author' of undefined
错误。我不太确定为什么
_editComment(event, key, value) {
this.setState({comments: this.state.comments.map((comment) => {
if (comment.id === key) {
comment.editing = false
comment.value = {value}
console.log('edited!')
}
})})
}
}
这是供参考的注释组件:
export default class Comment extends React.Component {
constructor() {
super();
this.state = {
isAbusive: false
};
}
render() {
let commentBody;
if (!this.state.isAbusive) {
commentBody = <div className='comment-body'>
<Editable
editing={this.props.editing}
value={this.props.value}
onEdit={this.props.onEdit}/>
</div>
} else {
commentBody = <em>Content marked as abusive</em>;
}
return(
<div className="comment">
<p className="comment-header">{this.props.author}</p>
{commentBody}
<div className="comment-actions">
<VotingButtons />
<a className='comment-actions-edit' href="#" onClick={this.props.onEditClick}>Edit comment</a>
<a className='comment-actions-delete' href="#" onClick={this.props.onDelete}>Delete comment</a>
<a className='comment-actions-abuse' href="#" onClick={this._toggleAbuse.bind(this)}>Report as abuse</a>
</div>
</div>
);
}
我强烈建议不要更改this.state.comments(就像您在地图中所做的那样),而是返回一个带有更新值的新实例。
_editComment(event, key, value) {
this.setState({
comments: this.state.comments.map((comment) => {
if (comment.id === key) {
return {
...comment,
editing: false,
value: value
}
}
return comment;
})
})
}
comment.value = value
而非comment.value = {value}
相关文章:
- 如果在编辑中下拉值发生变化,如何执行功能
- JS不工作:Sublime 3编辑器-Var main=函数()-=不是“”;激活“;(功能新手)
- 正在寻找具有多种功能的文本编辑器
- Wordpress自定义程序具有页面编辑功能
- 在我的项目中,添加、删除和编辑功能不起作用
- 如果我们在表格标题中有可编辑的文本,选项卡功能将无法正常工作
- 谷歌表格编辑功能
- CKFinder“编辑”功能在不同域上工作时不起作用
- 在 1 页而不是 2 页中添加编辑功能
- 剑道编辑器 - 缩略图 URL 的功能不起作用
- 无法向我的程序添加删除/编辑功能
- 具有表格生成功能的任何标记语言的编辑器
- 寻找特定的所见即所得编辑器功能.建议
- 如何使用 jQuery 编辑和删除表记录的功能更改 标签
- 编辑:I'我的按钮及其功能有问题
- 使用 JavaScript 和本地存储编辑功能
- 使用jQuery编辑功能
- jqGrid需要'输入'键来执行内联编辑的验证功能
- 谷歌地图api v3:为编辑后的多边形添加自定义撤消/重做功能
- 我如何扩展这个功能(编辑按钮)在JavaScript中的Odoo