feat: 修复评论数量问题
This commit is contained in:
@@ -249,13 +249,18 @@ export default forwardRef(function Comments(
|
||||
const [comments, setComments] = useState<Comment[]>([]);
|
||||
const inputRef = useRef<CommentInputRef>(null);
|
||||
|
||||
const commentCountUpdateRef = useRef()
|
||||
|
||||
useEffect(() => {
|
||||
getComments(1);
|
||||
}, [game_id]);
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
addComment: handleReply,
|
||||
getCommentCount: () => comments.length,
|
||||
getCommentCount: (onUpdate) => {
|
||||
commentCountUpdateRef.current = onUpdate
|
||||
onUpdate(comments.length)
|
||||
},
|
||||
}));
|
||||
|
||||
async function getComments(page) {
|
||||
@@ -270,6 +275,7 @@ export default forwardRef(function Comments(
|
||||
setComments((prev) => {
|
||||
const res = [...prev];
|
||||
res.splice(page * PAGESIZE - 1, newComments.length, ...newComments);
|
||||
commentCountUpdateRef.current?.(res.length)
|
||||
return res;
|
||||
});
|
||||
}
|
||||
@@ -297,6 +303,7 @@ export default forwardRef(function Comments(
|
||||
item.reply_count = res.data.count;
|
||||
}
|
||||
});
|
||||
commentCountUpdateRef.current?.(newComments.length)
|
||||
return newComments;
|
||||
});
|
||||
}
|
||||
@@ -318,6 +325,7 @@ export default forwardRef(function Comments(
|
||||
const res = await CommentServices.createComment({ game_id, content: val });
|
||||
if (res.code === 0) {
|
||||
setComments((prev) => {
|
||||
commentCountUpdateRef.current?.(prev.length + 1)
|
||||
return [{ ...res.data, replies: [] }, ...prev];
|
||||
});
|
||||
toast("发布成功");
|
||||
@@ -367,7 +375,7 @@ export default forwardRef(function Comments(
|
||||
});
|
||||
} else {
|
||||
setComments((prev) => {
|
||||
console.log(prev, parent_id, id);
|
||||
commentCountUpdateRef.current?.(prev.length - 1)
|
||||
return prev.filter((item) => item.id !== id);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user