node.js - 如何在 react.js 中获取这些注释数组的值?
问题描述
我想获得我在反应应用程序中的评论数组的值。这是发生了什么:我有来自我的节点 api 的帖子。每个帖子都有一系列评论。我映射到评论数组并将它们显示在我正在构建的博客应用程序的评论部分。用户能够删除他或她所做的评论是其中一项功能。因此,当用户选择要编辑的评论时,我还想获取当前文本值并将其显示在文本框中,以便用户可以根据需要进行编辑。我当前的代码是这样工作的:当用户单击编辑按钮时,它会更改为编辑模式,而不会获取注释框中的先前文本。这是我的代码:
import { useLocation } from 'react-router';
import React, {useEffect, useState, useContext} from 'react';
import axios from 'axios';
import {Context} from "../../context/Context"
import "./comment.css"
export default function Comments() {
const PF = "http://localhost:5000/images/";
const {user, getPost} = useContext(Context)
const location = useLocation()
const path = location.pathname.split("/")[2];
const [comments, setComments] = useState([]);
const [updateComment, setUpdateComment] = useState(false);
const [commentdescription2, setCommentDescription] = useState()
useEffect(() => {
const getPost = async () =>{ //I fetched the array of comments from the post
try{
const response = await axios.get("/posts/"+path )
setComments(response.data.comments)//it is assigned to comments as a useState
}catch(err){
console.log(err)
}
}
getPost()
}, [path])
//Function to handle comment update. Comments get updated with the new texts actually
//but old text values are not gotten on the textbox
const handleCommentUpdate = async (id) =>{
try{
await axios.put("/posts/"+ path + "/comment/" + id, {
author: user._id,
commentdescription: commentdescription2,
})
window.location.reload()
}catch(err){
console.log(err)
}
}
return (
<div className="comment">
<h5 className='users-comment'>Users comments</h5>
{comments.map((singleComment, index)=>{
//console.log(singleComment)
const {author, commentdescription, _id, createdAt} = singleComment
return(
<>
{updateComment == _id ?
<div className='comment-div' key={_id} >
<textarea className='comment-wrapper' type='text'
onChange={(e) => setCommentDescription(e.target.value)} value={commentdescription2}> //if I use commentdescription, I get the old text value but I cant edit it
//if I use commentdescription2, I can edit the textbox and update but have no access to the old comment text
</textarea>
<button className='comment-btn'onClick={() => handleCommentUpdate(_id)}>Update Post</button>
</div>
:
<div className='displayed-comments'key={_id}>
<div className="commentPic">
<img className="user-pics" src={PF + singleComment.author.profilePicture} alt="" />
</div>
<div className="comment-info">
<div className="comment-author-div">
<div className='comment-author-date-info'>
<h4 className="comment-author">{singleComment.author.username}</h4>
<p className="comment-date">{new Date(singleComment.createdAt).toDateString()}
</p>
</div>
<div className="comment-edit-delete-div">
<i className="singlePostIcon fas fa-edit" onClick={() => setUpdateComment(_id)} ></i>
<i className="singlePostIcon far fa-trash-alt" ></i>
</div>
</div>
<p className="comment-content"> {singleComment.commentdescription}</p>
</div>
</div>
}
</>
)
})}
</div>
)
}
解决方案
推荐阅读
- vba - 访问 SQL(或 VBA 代码)以列出具有至少一个附件字段的所有表
- javascript - 在 Javascript 中从没有 eval 的变量创建自定义代码
- python - 用 C 语言写入文件,同时在 python 中读取文件
- php - array1 包含 array2 php 纯循环和 if contion 中的所有值
- python - 在 Pandas 中防止广播
- python - 删除 s3 存储桶中的所有文件,但不是所有目录
- powershell - 从跨子域的所有 ServiceAccounts OU 获取帐户
- java - 如何将本地EngineeringCRS中的坐标转换为WGS84?
- javascript - 过滤数组,如果它有一个特定的键值对
- python-3.x - 从 pandas 数据框中为 pytorch lstm 准备数据的最有效方法