https://nomadcoders.co/carrot-market/lectures/4845
여기 댓글로 누군가 해결법 작성해줌
?? : getSession 과 관련해서 에러가 발생해서 이렇게 고쳤습니다
getSession 과 nextCache 를 같이 쓰면 안된다고 합니다 ~!
async function getLikeStatus(postId: number, userId: number) { // param추가
// const session = await getSession(); /*주석*/
const isLiked = await db.like.findUnique({
where: {
id: {
postId,
userId: userId, // sesson.userId!에서 session제거
},
},
});
const likeCount = await db.like.count({
where: {
postId,
},
});
return {
likeCount,
isLiked: Boolean(isLiked),
};
}
async function getCachedLikeStatus(postId: number) { // async
const session = await getSession(); // await session 추가
const userId = session.id // userId 추가
const cachedOperation = nextCache(getLikeStatus, ["product-like-status"], {
tags: [`like-status-${postId}`],
});
return cachedOperation(postId, userId!); //param에 추가
이 에러는
useOptimistic 훅의 사용 방식 때문에 발생합니다. React 19에서 useOptimistic은
트랜지션이나 액션 내에서 사용되어야 하는데, 현재 코드에서는 그렇지 않기 때문입니다.
이 문제를 해결하기 위해 다음과 같이 코드를 수정할 수 있습니다:
useTransition 훅을 import하고 사용합니다.onClick 함수를 startTransition 으로 감싸 트랜지션 내에서 optimistic 업데이트를 수행합니다.// 1.
import { useOptimistic, useTransition } from "react";
// 2.
const [isPending, startTransition] = useTransition();
const onClick = async () => {
startTransition(async () => {
reducerFn(undefined);
if (isLiked) {
await dislikePost(tweetId);
} else {
await likePost(tweetId);
}
})
};