我正在制作一个社交媒体应用程序。我正在循环浏览所有评论并将其显示在用户界面上。当我单击编辑时,最后一条评论的文本总是显示在输入中。我尝试了很多不同的事情,改变了结构,使用bind来绑定上下文,但没有任何帮助。
我正在使用 React Material UI。
这是代码:
render() {
const { anchorEl } = this.state;
const open = Boolean(anchorEl);
return(
<Panel>
<form noValidate autoComplete="off" onSubmit={this.onSubmit}>
<TextField
id={`comment${this.state.post_id}`}
name="comment"
fullWidth
placeholder="Comment here"
margin="normal"
value={this.state.comment}
onChange={this.handleChange}
InputProps={{
endAdornment : <InputAdornment position="end">
<IconButton onClick={this.resetTextField}>
<CloseIcon/>
</IconButton>
</InputAdornment>
}}
/>
</form>
{this.props.comments && this.props.comments.length > 0 &&
<RenderComments
comments={this.state.comments}
open={open}
anchorEl={this.state.anchorEl}
handleClick={this.handleClick}
handleClose={this.handleClose}
handleDelete={this.handleDelete}
handleEdit={this.handleEdit}
/>
}
</Panel>
)
}
Run Code Online (Sandbox Code Playgroud)
const RenderComments = (props) => {
return props.comments.map(comment =>
<CommentBase
key={comment.id}
comment={comment}
open={props.open}
anchorEl={props.anchorEl}
handleClick={props.handleClick}
handleClose={props.handleClose}
handleDelete={props.handleDelete}
handleEdit={props.handleEdit}
/>
);
};
Run Code Online (Sandbox Code Playgroud)
const CommentBase = ({comment, open, anchorEl, handleClick, handleClose, handleDelete, handleEdit}) => (
<CommentBasePanel>
<CommentText>
{comment.text}
<span style={{ float: 'right', fontSize: 10, color: '#A9A9A9' }}>{moment(comment.created_at).fromNow()}</span>
</CommentText>
<HelperAction>
<MoreVertIcon
id={comment.id}
aria-owns={open ? `simple-popper${comment.id}` : null}
aria-haspopup="true"
variant="contained"
onClick={handleClick}
/>
<Popover
id={`simple-popper${comment.id}`}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
>
<Typography style={{ padding: 10, cursor: 'pointer' }} onClick={() => handleEdit(comment)}>
Edit
</Typography>
<Typography style={{ padding: 10, color: red[500], cursor: 'pointer' }} onClick={() => handleDelete(comment.id)}>
Delete
</Typography>
</Popover>
</HelperAction>
</CommentBasePanel>
);
Run Code Online (Sandbox Code Playgroud)
handleEdit = (comment) => {
this.setState({ comment: comment.text, comment_id: comment.id })
};
Run Code Online (Sandbox Code Playgroud)
无论我编辑什么评论,handleEdit 方法中的控制台登录评论始终会记录最后一条评论。正如您在图片中看到的那样,编辑第一条评论会给出最后一条评论文本。
糟糕的 Popovers 管理
map
将相同的open
和anchorEl
道具复制到所有 Popover 实例 - handleClick
(您没有显示这一点)*在同一位置打开所有这些实例**(最后一个位于顶部)。
修复:包含id
在handleClick
、保存在状态中并在open
属性条件中使用
FIX2:您可以使用一个<Popover/>
实例,尤其是在不显示与特定评论相关的任何内容时。
附言。我在 stackblitz 中花费了更多的时间来重新创建它(猜测丢失的部分),而不是真正的调试(实际上只检查<Popover/>
渲染的html 结构{comment.id}
)。下次展示更完整的代码或提供最小的工作示例。
RenderComments
如果您按如下方式更新渲染方法,它应该可以解决您的问题:
const RenderComments = (props) => {
return props.comments.map(comment =>
<CommentBase
key={comment.id}
comment={comment}
open={props.open}
anchorEl={props.anchorEl}
handleClick={ props.handleClick }
handleClose={ props.handleClose }
handleDelete={ () => props.handleDelete(comment.id) }
handleEdit={ () => props.handleEdit(comment) }
/>
);
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2111 次 |
最近记录: |