Basically, I want to update the comments for a post when the user adds a new comment. However, when the "handleClick" function is called the component is not rerendered with the updated comments and I get the following error:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
I am very confused why this is and I have been looking everywhere for a solution. The complete code for the component:
import React, { useState, useRef } from 'react';
import { Typography, TextField, Button } from '@material-ui/core';
import { useDispatch, useSelector } from 'react-redux';
import useStyles from './styles';
import { commentPost } from '../../redux/reducers/posts';
const CommentSection = ({ post }) => {
const classes = useStyles();
const [comments, setComments] = useState(post?.comments);
const [comment, setComment] = useState("");
const user = JSON.parse(localStorage.getItem('profile'));
const dispatch = useDispatch();
const handleClick = async () => {
const finalComment = `${user.result.name}: ${comment}`;
const newComments = await dispatch(commentPost({ value: finalComment, id: post._id }));
setComments(newComments?.payload?.comments);
setComment('');
};
return (
<div>
<div className={classes.commentsOuterContainer}>
<div className={classes.commentsInnerContainer}>
<Typography gutterBottom variant="h6">Comments</Typography>
{comments.map((c, i) => (
<Typography key={i} gutterBottom variant="subtitle1">
{c}
</Typography>
))}
</div>
{user?.result?.name && (
<div style={{ width: '70%' }}>
<Typography gutterBottom variant="h6">Write a Comment</Typography>
<TextField fullWidth minRows={4} variant="outlined" label="Comment" multiline value={comment} onChange={(e) => setComment(e.target.value)} />
<Button style={{marginTop: '10px'}} fullWidth disabled={!comment} variant="contained" color="primary" onClick={handleClick}>
Comment
</Button>
</div>
)}
</div>
</div>
);
};
export default CommentSection;