reactjs - React map 总是使用最后一个元素的数据调用方法

标签 reactjs es6-map

我正在制作一个社交媒体应用程序。我正在浏览所有评论并将其显示在 UI 上。当我点击编辑时,最后一条评论的文本总是出现在输入中。我尝试了很多不同的东西,改变了结构,使用 bind 来绑定(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>
    )
}
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}
        />
    );
};
    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>
);
handleEdit = (comment) => {
    this.setState({ comment: comment.text, comment_id: comment.id })
};

无论我编辑什么评论,handleEdit 方法中的控制台登录评论始终会记录最后一条评论。编辑第一条评论给出最后一条评论文本,如您在图像中看到的那样。

enter image description here

最佳答案

错误的 Popovers 管理
map复制相同 openanchorEl所有 Popover 实例的 Prop - handleClick (你没有显示这个)*在同一个地方打开所有这些**(最后一个在顶部)。

修复:包括 idhandleClick ,保存状态和使用状态为open属性(property)

FIX2:您可以使用一个 <Popover/>尤其是在不显示与特定评论相关的任何内容时。

附注。我花了更多的时间在 stackblitz 中重新创建这个(猜测丢失的部分)而不是真正的调试(实际上只检查 <Popover/> 的 html 结构和渲染 {comment.id} )。下次展示更完整的代码或提供最少的工作示例。

关于reactjs - React map 总是使用最后一个元素的数据调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52266311/

相关文章:

reactjs - Uncaught Error : Cannot find module '@fortawesome/free-regular-svg-icons'

dictionary - 将 ES6 Map 与 Flow 类型一起使用

ecmascript-6 - 在 Aurelia 的 ES 6 map 上使用值转换器

javascript - 通过使用 Map Filter 与对象数组进行比较来返回字段

reactjs - 自动添加当前用户的地理位置

javascript - react 堆栈导航

javascript - React Router 的 v6 history.push ('/' ) 给出错误 "undefined (reading ' pathname')"- url 更改,但页面保持不变

javascript - React Native 中的 Object.keys 顺序

javascript - 将键组合到由基元和对象组成的 ES6 Map