Slide 38
Slide 38 text
useOptimisticの動作例
const [likes, setLikes] = useState(0);
const [dispLikes, addOptimistic] = useOptimistic(
likes,
(currentLikes, optimisticValue) => {
return currentLikes + optimisticValue;
}
);
startTransition(async () => {
addOptimistic(1);
const newLikes = await api.postLike();
setLikes(newLikes);
});
likes = 10
optimisticValue = 1
① 非同期トランジション開始
→ addOptimistic呼び出し
dispLikes = 10 + 1 = 11
⇒