【发布时间】:2020-11-25 06:24:11
【问题描述】:
我正在尝试一个 MERN 博客网站项目。目前,我在更新帖子评论时遇到问题。我正在编辑来自另一个组件的评论,但在更新评论后我无法更新状态。任何人都可以帮助我谢谢。
EditComment 组件的客户端代码:
import React, { Fragment, useState } from 'react';
import Input from '../components/Input';
import Axios from 'axios';
export const EditComment = (props) => {
const [commentBody, setComment] = useState(props.comment.commentBody);
console.log(props.comment._id)
const [commentState, setCommentState] = useState([props.cmtState]);
const handleChange = (event) => {
const { value, name } = event.target
setComment(value)
}
console.log(commentBody)
const handleCommentEdit = async (postid, commentid) => {
try {
const res = await Axios.patch(`/api/posts/${postid}/comments/${commentid}`,JSON.stringify({commentBody}), {
headers: {
"Content-Type": "application/json",
'Authorization': 'Bearer ' +localStorage.getItem('token')
}
})
setCommentState(prevValue => {
return [...prevValue, res.data.info]
});
console.log(res)
} catch (error) {
console.log( error.response.request );
}
}
return(
<Fragment>
<i
data-toggle="modal" data-target={`#exampleModal${props.comment._id}`}
className="fa fa-pencil ml-3"
style={{color: "blue", cursor: 'pointer'}}></i>
<div
className="modal fade"
id={`exampleModal${props.comment._id}`}
tabIndex="-1" role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div className="modal-dialog" role="document">
<div className="modal-content">
<div className="modal-header">
<h5
className="modal-title"
id="exampleModalLabel">Modal title</h5>
<button
type="button"
className="close"
data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div className="modal-body">
<Input
type="text"
name="commentBody"
value={commentBody}
onChange={ handleChange }
className="form-control" />
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-secondary"
data-dismiss="modal">Close</button>
<button
type="button"
onClick={() => handleCommentEdit(props.postid, props.comment._id)}
data-dismiss="modal"
className="btn btn-warning">Save changes</button>
</div>
</div>
</div>
</div>
</Fragment>
)
}
显示评论的客户端代码(需要显示更新状态的地方):
const [commentBody, setComment] = useState('');
const [commentState, setCommentState] = useState([]);
<h6>Comments</h6>
{
commentState.length > 0 ? commentState.map((comment, index) => {
return(
<div key={index} className="mb-2">
<span className="mr-2">{comment.commentBody}</span>
<span className="mr-2 text-primary"><em>-{comment.commentBy.name}</em></span>
<span className="text-secondary"><em>{formatDate(comment.createdAt)}</em></span>
{
authContext.userState.user && authContext.userState.user._id === comment.commentBy._id &&
<Fragment>
<EditComment cmtState={commentState} comment={comment} postid={postid}/>
<i
className="fa fa-trash ml-3"
onClick={() => handleCommentDelete(postid, comment._id)}
style={{color: "red", cursor: 'pointer'}}></i>
</Fragment>
}
</div>
)
}) :""
}
【问题讨论】:
-
您看到任何错误吗?另外,如果可能的话,您可以共享代码的沙箱链接。是否要在编辑评论发生时更新父组件状态?
-
没有错误。但是当我从 EditComment 组件编辑评论时,它不会更新 DisplayPost 的状态(显示所有评论)。是的,当然。
-
codesandbox.io/s/restless-star-qgi2w@HarmandeepSinghKalsi
-
您似乎已经上传了整个项目 :) 它应该只是显示如何复制的最少代码。无论如何,之后如何登录和复制?
-
@tmhao2005 抱歉,#Harmandeep 想查看代码。
标签: javascript node.js arrays reactjs api