fix(lint): avoid effect self-references in async loaders
This commit is contained in:
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user