组件不使用更新的状态重新读取
基本上,当用户添加新评论时,我想更新帖子的评论。但是,当称为“ HandleClick”功能时,该组件不会带有更新的注释,并且我会收到以下错误:
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.
我很困惑为什么这是为什么,我一直在寻找解决方案。组件的完整代码:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论