gpt4 book ai didi

javascript - 警告 : Can't perform a React state update on an unmounted component

转载 作者:行者123 更新时间:2023-12-03 06:59:10 24 4
gpt4 key购买 nike

我收到以下警告:
" 警告:无法对卸载的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要修复,请取消 useEffect 清理函数中的所有订阅和异步任务.
在 AddComment 中(在 CommentCard.js:50)
在 div 中(由 Comment
创建“(来自 CommentCard 的第 50 行是 AddComment 组件所在的行)
我有 留言卡 的帮助下显示注释的组件评论 来自 Ant 设计的组件。我使用 Comment 组件的 children 属性来显示 添加评论 特定评论的组件。
添加评论 组件添加对评论的回复。
显示 添加评论 相应评论的组件我使用了一个状态数组,我只显示状态等于 1 的评论的组件。
添加回复后,我想要 添加评论 要删除的组件。为此,我在成功添加回复后更改了评论的状态。我在发布回复后立即收到警告。
这是我的 CommentCard 组件

function CommentCard(props) {
const [hasReplyCommentBox, setHasReplyCommentBox] = useState([]);
const { t } = useTranslation();

const commentStyle = {
padding: '10px',
backgroundColor: 'white',
'whiteSpace': 'pre',
width: '100%'
};

function toggleReplyBoxVisibility(commentIndex) {
var auxState = { ...hasReplyCommentBox };
auxState[commentIndex] = auxState[commentIndex] ? 0 : 1;
setHasReplyCommentBox(auxState);
}

const actions = [
<span
id={"reply-button-" + props.commentIndex}
onClick={() => toggleReplyBoxVisibility(props.commentIndex)}>
{t('Reply to')}
</span>
];

const commentReplyBox = (
hasReplyCommentBox[props.commentIndex]
? <AddComment
id={props.codeId}
parentCommentId={props.parentCommentId}
commentIndex={props.commentIndex}
toggleReplyBoxVisibility={toggleReplyBoxVisibility}
updateComments={props.updateComments}
/>
: null
);

return (
<Comment
author={props.userId}
datetime={props.datePosted}
content={props.body}
actions={actions}
children={commentReplyBox}
style={commentStyle}
/>
);
}
这是我的 AddComment 组件:
function AddComment(props) {
const { t } = useTranslation();
const { TextArea } = Input;
const [form] = Form.useForm();
const [comment, setComment] = useState();

const buttonStyle = { float: 'right' };

function onCommentChange(newComment) {
setComment(newComment.target.value);
}

function resetCommentInput() {
setComment('');
}

function onFormReset() {
form.resetFields();
}

function submitComment() {
let request = {
body: comment,
code_id: props.id,
line_number: props.lineNumber,
parent_comment_id: props.parentCommentId
};
fetch('/api/comment/add',
{
method: 'POST',
body: JSON.stringify(request)
}
).then(response => response.json())
.then(data => {
if (data.success === 1) {
if (props.parentCommentId) {
props.toggleReplyBoxVisibility(props.commentIndex);
}
props.updateComments();
resetCommentInput();
}
});
}

return (
<>
<Form form={form} name="comment" className="comment-form"
onFinish={submitComment}
id={"add-comment-form" + props.parentCommentId}>
<Form.Item name="body" label={t('Comment')}>
<TextArea placeholder={t('Leave a comment')}
onChange={onCommentChange}
id={getCommentTextAreaId(props.lineNumber, props.parentCommentId)}
/>
</Form.Item>
<Form.Item style={buttonStyle}>
<Space>
<Button type="primary" htmlType="submit"
id={
getPostCommentButtonId(props.lineNumber, props.parentCommentId)
}
className = "comment-form-button" onClick={onFormReset}>
{t('Post')}
</Button>
{props.parentCommentId
? <Button id={"cancel-add-reply-comment-" + props.parentCommentId}
type="secondary" className="comment-form-button"
onClick={
() => props.toggleReplyBoxVisibility(props.commentIndex)
}>
{t('Cancel')}
</Button>
: null
}
</Space>
</Form.Item>
</Form>
</>
);
}

最佳答案

尝试这个

function submitComment() {
let request = {
body: comment,
code_id: props.id,
line_number: props.lineNumber,
parent_comment_id: props.parentCommentId
};
fetch('/api/comment/add',
{
method: 'POST',
body: JSON.stringify(request)
}
).then(response => response.json())
.then(data => {
if (data.success === 1) {
props.updateComments();
resetCommentInput();
// Run resetCommentInput before props.toggleReplyBoxVisibility
if (props.parentCommentId) {
props.toggleReplyBoxVisibility(props.commentIndex);
}
}
});
}
您应该在卸载之前更新组件状态

关于javascript - 警告 : Can't perform a React state update on an unmounted component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64804801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com