fix(lint): avoid effect self-references in async loaders

This commit is contained in:
zetaloop
2026-04-24 09:06:55 +08:00
parent d59f7c6dc8
commit 774c62ec1c
3 changed files with 96 additions and 42 deletions
+36 -14
View File
@@ -24,22 +24,44 @@ export function PostComments({ postId }: PostCommentsProps) {
const [pendingLike, setPendingLike] = useState<Record<string, boolean>>({})
const { requireAuth } = useRequireAuth()
const refresh = useCallback(async () => {
setLoading(true)
setLoadError(null)
try {
const items = await listCommentsByPost(postId)
setComments(items)
} catch (err: unknown) {
setLoadError(toApiError(err).msg)
} finally {
setLoading(false)
}
}, [postId])
const refresh = useCallback(
async (showLoading = true) => {
if (showLoading) {
setLoading(true)
setLoadError(null)
}
try {
const items = await listCommentsByPost(postId)
setComments(items)
} catch (err: unknown) {
setLoadError(toApiError(err).msg)
} finally {
setLoading(false)
}
},
[postId],
)
useEffect(() => {
void refresh()
}, [refresh])
let cancelled = false
void (async () => {
try {
const items = await listCommentsByPost(postId)
if (cancelled) return
setComments(items)
} catch (err: unknown) {
if (cancelled) return
setLoadError(toApiError(err).msg)
} finally {
if (!cancelled) setLoading(false)
}
})()
return () => {
cancelled = true
}
}, [postId])
return (
<div className="space-y-4">