I want to update the component after adding a new comment which gets stored into the backend and updates the previously displayed component with the new one which has a new comment.
Here is the Page.js which loads a single post/article and its comments:
import React ,{useState, useContext, useEffect}from 'react';
import {useHistory,Redirect, useParams} from 'react-router-dom'
import axios from 'axios'
import '../CSS/Page.css'
import CreateComment from './CreateComment';
const Page=()=>{
const [show,setShow]=useState({
commentBox:false
})
const [post,setPost] = useState(
{id:'', username:'', title:'',body:'',date:'',comments:[{}]
})
let {postTitle}=useParams()
useEffect(() => {
axios.get(`http://localhost:2000/apiEndpoint/singlePost/${postTitle}`,{withCredentials:true},{
headers: {
'Content-Type': 'application/json'
}
}).then((res)=>{
console.log(res.data)
const postS=res.data
setPost({...post,id:postS._id, username:postS.username, title:postS.title, body:postS.body,comments: postS.comments})
return;
}).catch((err)=>{
console.log([err])
})
},[]);
const handleCommentButton=(e)=>{
setShow({ ...show, commentBox:!show.commentBox });
}
return (
<div className='postContainer'>
<div className='singlePostcontainer'>
<div className='singlePost' >
<h1>{post.title}</h1>
<hr/>
<p>{post.body} </p>
{post.id}
<hr/>
<h5>Comments:<button className='btnCom' onClick={handleCommentButton}>{show.commentBox?'Close':'Add Comment'}</button></h5>
{show.commentBox?(<CreateComment post={post} />):''}
{post.comments.map(comment=>{
const commentID=comment._id
return(
<div className='comment' key={commentID}>
<h3>{comment.body}</h3>
<h6>By: {comment.creater}</h6>
</div>
)
})}
</div>
</div>
</div>
)
}
export default Page
Here is the CreateComment.js component which has a form and post request to the database:
import React, { Component,useState, useEffect, lazy } from 'react';
import Cookies from 'js-cookie';
import { Link, Redirect } from 'react-router-dom';
const axios = require('axios').default;
const CreateComment=(props)=>{
var commentStr={
body:''
}
const [comment, setComment] = useState(commentStr);
const handleSubmitA=(e)=>{
e.preventDefault()
console.log('This is the id:',props.post.id)
axios.post(`http://localhost:2000/apiEndpoint/CREATE/comment/${props.post.id}`,{
body:comment.body
},
{withCredentials:true},{
headers: {
'Content-Type': 'application/json'
}}).then(res=>{
console.log(res);
})
}
const handleChangeA=(e)=>{
const {name,value}=e.target
setComment({ ...comment, [name]: value });
}
return(
<div className='commentContainer'>
<form onSubmit={handleSubmitA}>
<label>Enter Comment</label>
<textarea name="body" onChange={handleChangeA}></textarea>
<button>Submit</button>
</form>
</div>
)
}
export default CreateComment
I'm able to add component successfully and it is also posting the comment to the backend database also. But it only shows on the page if I press reload.
I removed the empty array from inside of useEffect hook. It gives me an infinite loop but does the work correctly. However as that is not a good practice and it takes up local storage resources, how can I perform the same task but without the infinite loop?