【发布时间】:2021-02-24 12:14:53
【问题描述】:
我收到以下警告: "警告:无法对未安装的组件执行 React 状态更新。这是一个无操作,但它表明您的应用程序中存在内存泄漏。要修复,请在 useEffect 清理函数中取消所有订阅和异步任务. 在 AddComment 中(在 CommentCard.js:50) in div(由 Comment 创建”(CommentCard 的第 50 行是 AddComment 组件所在的行)
我有 CommentCard 组件,它在 ant design 的 Comment 组件的帮助下显示评论。我使用 Comment 组件的 children 属性来显示特定评论的 AddComment 组件。
AddComment 组件添加对评论的回复。 为了显示相应评论的 AddComment 组件,我使用了一个状态数组,并且我只为状态等于 1 的 cmets 显示该组件。
添加回复后,我希望删除 AddComment 组件。为此,我在成功添加回复后更改了评论的状态。我在发布回复后立即收到警告。
这是我的 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>
</>
);
}
【问题讨论】:
标签: javascript reactjs react-native rxjs