React์ useOptimistic ํ ์ ์ฌ์ฉํ์ฌ ๋๊ด์ ์ ๋ฐ์ดํธ๋ก ๋ ๋ถ๋๋ฝ๊ณ ๋ฐ์์ฑ์ด ๋ฐ์ด๋ ์ฌ์ฉ์ ๊ฒฝํ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋๋ค. ์ค์ ์์ ์ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ดํด๋ณด์ธ์.
React useOptimistic: ๋๊ด์ ์ ๋ฐ์ดํธ์ ๋ํ ์ข ํฉ ๊ฐ์ด๋
์น ๊ฐ๋ฐ ์ธ๊ณ์์ ๋ฐ์์ฑ์ด ์ข๊ณ ๋งค๋ ฅ์ ์ธ ์ฌ์ฉ์ ๊ฒฝํ์ ๋ง๋๋ ๊ฒ์ด ๊ฐ์ฅ ์ค์ํฉ๋๋ค. ์ด๋ฅผ ๋ฌ์ฑํ๋ ํต์ฌ ๊ธฐ์ ์ค ํ๋๋ ๋๊ด์ ์
๋ฐ์ดํธ์
๋๋ค. React 18์ ๋์
๋ React์ useOptimistic
ํ
์ ์ด ํจํด์ ๊ตฌํํ๋ ๊ฐ์ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์ด ๊ฐ์ด๋์์๋ useOptimistic
์ ์ด์ , ์ฌ์ฉ ์ฌ๋ก ๋ฐ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์์ธํ ์ดํด๋ด
๋๋ค.
๋๊ด์ ์ ๋ฐ์ดํธ๋ ๋ฌด์์ ๋๊น?
๋๊ด์ ์ ๋ฐ์ดํธ๋ ์๋ฒ์์ ํ์ธ์ ์ค์ ๋ก ๋ฐ๊ธฐ ์ ์ ๋น๋๊ธฐ ์์ (์: ์๋ฒ์ ๋ํ ๋คํธ์ํฌ ์์ฒญ)์ด ์ฑ๊ณตํ ๊ฒ์ฒ๋ผ ์ฌ์ฉ์ ์ธํฐํ์ด์ค(UI)๋ฅผ ์ ๋ฐ์ดํธํ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ฆ๊ฐ์ ์ธ ํผ๋๋ฐฑ์ด๋ผ๋ ์ฐฉ๊ฐ์ ์ผ์ผ์ผ ์ฌ์ฉ์์ ์๋ต์ฑ์ ๋ํ ์ธ์์ ํฌ๊ฒ ํฅ์์ํต๋๋ค. ์์ ์ด ๋์ค์ ์คํจํ๋ฉด UI๋ ์๋ ์ํ๋ก ๋๋์๊ฐ๋๋ค.
์ฌ์ฉ์๊ฐ ๊ฒ์๋ฌผ์ "์ข์์"๋ฅผ ๋๋ฅผ ์ ์๋ ์์ ๋ฏธ๋์ด ์ ํ๋ฆฌ์ผ์ด์ ์ ์๊ฐํด ๋ณด์ธ์. ๋๊ด์ ์ ๋ฐ์ดํธ๊ฐ ์์ผ๋ฉด ์ข์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์๋ฒ์ ๋ํ ์์ฒญ์ด ํธ๋ฆฌ๊ฑฐ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ์๋ฒ์์ ์ข์์๋ฅผ ํ์ธํ ๋๊น์ง UI์ ๋ก๋ฉ ์ํ(์: ์คํผ๋)๊ฐ ํ์๋ฉ๋๋ค. ํนํ ๋๊ธฐ ์๊ฐ์ด ๊ธด ๋คํธ์ํฌ์์๋ ๋๋ฆฌ๊ณ ๋ฒ๊ฑฐ๋กญ๊ฒ ๋๊ปด์ง ์ ์์ต๋๋ค.
๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ ๋ ๊ฒ์๋ฌผ์ด ์ข์์ ํ์๋ ๊ฒ์ผ๋ก UI๊ฐ ์ฆ์ ์ ๋ฐ์ดํธ๋ฉ๋๋ค. ์๋ฒ์ ๋ํ ์์ฒญ์ ์ฌ์ ํ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ฐ์ํฉ๋๋ค. ์์ฒญ์ด ์ฑ๊ณตํ๋ฉด ์๋ฌด๊ฒ๋ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์์ฒญ์ด ์คํจํ๋ฉด(์: ๋คํธ์ํฌ ์ค๋ฅ ๋๋ ์๋ฒ ๋ฌธ์ ๋ก ์ธํด) UI๊ฐ ์๋ ์ํ๋ก ๋๋์๊ฐ๊ณ ์ฌ์ฉ์์๊ฒ ์ค๋ฅ ๋ฉ์์ง๊ฐ ํ์๋ ์ ์์ต๋๋ค.
๋๊ด์ ์ ๋ฐ์ดํธ์ ์ด์
- ํฅ์๋ ์ฌ์ฉ์ ๊ฒฝํ: ๋๊ด์ ์ ๋ฐ์ดํธ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ ๋น ๋ฅด๊ณ ์๋ต์ฑ์ด ๋ฐ์ด๋ ๋๋์ ์ฃผ์ด ๋์ฑ ๋ง์กฑ์ค๋ฌ์ด ์ฌ์ฉ์ ๊ฒฝํ์ ์ ๊ณตํฉ๋๋ค.
- ์ธ์๋๋ ๋๊ธฐ ์๊ฐ ๊ฐ์: UI๋ฅผ ์ฆ์ ์ ๋ฐ์ดํธํ์ฌ ๋คํธ์ํฌ ์์ฒญ ๋ฐ ๊ธฐํ ๋น๋๊ธฐ ์์ ๊ณผ ๊ด๋ จ๋ ๋๊ธฐ ์๊ฐ์ ๊ฐ๋ฆฝ๋๋ค.
- ์ฌ์ฉ์ ์ฐธ์ฌ๋ ์ฆ๊ฐ: ๋ฐ์์ฑ์ด ๋ฐ์ด๋ UI๋ ์ฌ์ฉ์๊ฐ ์ ํ๋ฆฌ์ผ์ด์ ๊ณผ ๋ ๋ง์ด ์ํธ ์์ฉํ๋๋ก ์ฅ๋ คํฉ๋๋ค.
useOptimistic
์๊ฐ
useOptimistic
ํ
์ React์์ ๋๊ด์ ์
๋ฐ์ดํธ ๊ตฌํ์ ๋จ์ํํฉ๋๋ค. ๋ ๊ฐ์ง ์ธ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ์ด๊ธฐ ์ํ: ๋๊ด์ ์ผ๋ก ์ ๋ฐ์ดํธํ๋ ค๋ ์ํ์ ์ด๊ธฐ ๊ฐ์ ๋๋ค.
- ์ ๋ฐ์ดํธ ํจ์: ํ์ฌ ์ํ์ ๋๊ด์ ์ ๋ฐ์ดํธ ๊ฐ์ ์ ๋ ฅ์ผ๋ก ๋ฐ์ ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์ ์ฉํ ํ์ ์ ์ํ๋ฅผ ๋ฐํํ๋ ํจ์์ ๋๋ค.
์ด ํ ์ ๋ค์์ ํฌํจํ๋ ๋ฐฐ์ด์ ๋ฐํํฉ๋๋ค.
- ํ์ฌ ์ํ: ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ๋ฐ์ํ๋ ์ํ์ ๋๋ค.
- ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์ ์ฉํ๋ ํจ์: ์ด ํจ์๋ ๋๊ด์ ์ ๋ฐ์ดํธ ๊ฐ์ ์ ๋ ฅ์ผ๋ก ๋ฐ์ ์ ๋ฐ์ดํธ๋ ์ํ๋ก ๋ค์ ๋ ๋๋ง์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
๊ธฐ๋ณธ ์์ : ๊ฒ์๋ฌผ ์ข์์
์์
๋ฏธ๋์ด ์์ ๋ฅผ ๋ค์ ๋ฐฉ๋ฌธํ์ฌ useOptimistic
์ ์ฌ์ฉํ์ฌ ๋๊ด์ ์ธ ์ข์์๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์์๋ด
๋๋ค.
import React, { useState, useOptimistic } from 'react';
function Post({ postId, initialLikes }) {
const [isLiking, setIsLiking] = useState(false);
const [optimisticLikes, addOptimisticLike] = useOptimistic(
initialLikes,
(state, optimisticUpdate) => state + optimisticUpdate
);
const handleLike = async () => {
setIsLiking(true);
addOptimisticLike(1);
try {
// Simulate an API call to like the post
await new Promise((resolve) => setTimeout(resolve, 500)); // Simulate network latency
// await api.likePost(postId); // Replace with your actual API call
} catch (error) {
console.error("Failed to like post:", error);
addOptimisticLike(-1); // Revert the optimistic update
// Optionally, display an error message to the user
} finally {
setIsLiking(false);
}
};
return (
<div>
<p>Likes: {optimisticLikes}</p>
<button onClick={handleLike} disabled={isLiking}>
{isLiking ? "Liking..." : "Like"}
</button>
</div>
);
}
export default Post;
์ค๋ช :
useOptimistic
์ ๊ฒ์๋ฌผ์initialLikes
์๋ก ์ด๊ธฐํํฉ๋๋ค.- ์
๋ฐ์ดํธ ํจ์๋ ๋จ์ํ
optimisticUpdate
(1 ๋๋ -1์ด ๋จ)๋ฅผ ํ์ฌstate
(์ข์์ ์)์ ์ถ๊ฐํฉ๋๋ค. - ์ฌ์ฉ์๊ฐ ์ข์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด
addOptimisticLike(1)
์ ํธ์ถํ์ฌ UI์์ ์ข์์ ์๋ฅผ ์ฆ์ ๋๋ฆฝ๋๋ค. - ๊ทธ๋ฐ ๋ค์ ์๋ฒ์์ ๊ฒ์๋ฌผ์ ์ข์์๋ฅผ ํ์ํ๊ธฐ ์ํด API ํธ์ถ์ ํฉ๋๋ค(์ด ์์์๋
setTimeout
์ผ๋ก ์๋ฎฌ๋ ์ด์ ๋จ). - API ํธ์ถ์ด ์ฑ๊ณตํ๋ฉด ์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์์ต๋๋ค. UI๋ ๋๊ด์ ์ข์์๋ก ์ ๋ฐ์ดํธ๋ ์ํ๋ก ์ ์ง๋ฉ๋๋ค.
- API ํธ์ถ์ด ์คํจํ๋ฉด
addOptimisticLike(-1)
์ ํธ์ถํ์ฌ ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ๋๋๋ฆฌ๊ณ ์ฌ์ฉ์์๊ฒ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
๊ณ ๊ธ ์์ : ๋๊ธ ์ถ๊ฐ
๋๊ด์ ์ ๋ฐ์ดํธ๋ ๋๊ธ ์ถ๊ฐ์ ๊ฐ์ด ๋ ๋ณต์กํ ์์ ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import React, { useState, useOptimistic } from 'react';
function CommentSection({ postId, initialComments }) {
const [newCommentText, setNewCommentText] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [optimisticComments, addOptimisticComment] = useOptimistic(
initialComments,
(state, optimisticComment) => [...state, optimisticComment]
);
const handleAddComment = async (e) => {
e.preventDefault();
if (!newCommentText.trim()) return;
setIsSubmitting(true);
const optimisticComment = { id: Date.now(), text: newCommentText, author: 'You (Optimistic)' };
addOptimisticComment(optimisticComment);
setNewCommentText('');
try {
// Simulate an API call to add the comment
await new Promise((resolve) => setTimeout(resolve, 500)); // Simulate network latency
// const newComment = await api.addComment(postId, newCommentText); // Replace with your actual API call
// In a real implementation, you'd replace the optimistic comment with the actual comment
// addOptimisticComment(newComment) // Example:
} catch (error) {
console.error("Failed to add comment:", error);
// Revert the optimistic update (remove the last comment)
addOptimisticComment(null); // Use a special value to signal removal.
//optimisticComments.pop(); // This will not trigger a re-render
// Optionally, display an error message to the user
} finally {
setIsSubmitting(false);
}
};
return (
<div>
<h3>Comments</h3>
<ul>
{optimisticComments.map((comment) => (
comment ? <li key={comment.id}>{comment.text} - {comment.author}</li> :
null // Render nothing if null comment. Handle cases where comment addition failed
))}
</ul>
<form onSubmit={handleAddComment}>
<input
type="text"
value={newCommentText}
onChange={(e) => setNewCommentText(e.target.value)}
placeholder="Add a comment..."
disabled={isSubmitting}
/>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Submitting..." : "Add Comment"}
</button>
</form>
</div>
);
}
export default CommentSection;
์ค๋ช :
useOptimistic
์initialComments
๋ฐฐ์ด๋ก ์ด๊ธฐํํฉ๋๋ค.- ์
๋ฐ์ดํธ ํจ์๋
optimisticComment
๋ฅผstate
(๋๊ธ ๋ฐฐ์ด)์ ์ถ๊ฐํฉ๋๋ค. - ์ฌ์ฉ์๊ฐ ์ ๋๊ธ์ ์ ์ถํ๋ฉด ์์ ID์ ์ฌ์ฉ์ ์
๋ ฅ์ด ํฌํจ๋
optimisticComment
๊ฐ์ฒด๋ฅผ ๋ง๋ญ๋๋ค. addOptimisticComment(optimisticComment)
๋ฅผ ํธ์ถํ์ฌ ๋๊ด์ ๋๊ธ์ UI์ ์ฆ์ ์ถ๊ฐํฉ๋๋ค.- ๊ทธ๋ฐ ๋ค์ ์๋ฒ์ ๋๊ธ์ ์ถ๊ฐํ๊ธฐ ์ํด API ํธ์ถ์ ํฉ๋๋ค(
setTimeout
์ผ๋ก ์๋ฎฌ๋ ์ด์ ๋จ). - API ํธ์ถ์ด ์ฑ๊ณตํ๋ฉด ์ค์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ์์ ๋๊ธ์ ์ฌ๋ฐ๋ฅธ ๋๊ธ๋ก ๋ฐ๊ฟ๋๋ค(์ ์ถ ํ ์์ ๋จ).
- API ํธ์ถ์ด ์คํจํ๋ฉด
addOptimisticComment(null)
์ ํธ์ถํ์ฌ ๋ง์ง๋ง ๋๊ธ(๋๊ด์ ์ธ ๋๊ธ์ด์๋)์ ์ ๊ฑฐํ์ฌ ์๋ ์ํ๋ก ๋๋๋ฆฝ๋๋ค. - ๋๊ธ ์ถ๊ฐ๊ฐ ์คํจํ ๊ฒฝ์ฐ(
comment ? <li ...> : null
)๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
useOptimistic
์ฌ์ฉ์ ์ํ ๋ชจ๋ฒ ์ฌ๋ก
- ์ค๋ฅ๋ฅผ ์ ์์ ์ผ๋ก ์ฒ๋ฆฌ: ํ์ํ ๊ฒฝ์ฐ ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ๋๋๋ฆฌ๊ธฐ ์ํด ๋น๋๊ธฐ ์์ ์ ํญ์ ์ค๋ฅ ์ฒ๋ฆฌ๋ฅผ ํฌํจํฉ๋๋ค. ์ฌ์ฉ์์๊ฒ ์ ์ตํ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํ์ํฉ๋๋ค.
- ์๊ฐ์ ํผ๋๋ฐฑ ์ ๊ณต: ๋๊ด์ ์ ๋ฐ์ดํธ๊ฐ ์งํ ์ค์ผ ๋ ์ฌ์ฉ์์๊ฒ ๋ช ํํ๊ฒ ์๋ฆฝ๋๋ค. ์ด๊ฒ์ ๋ฏธ๋ฌํ ์๊ฐ์ ์ ํธ(์: ๋ค๋ฅธ ๋ฐฐ๊ฒฝ์ ๋๋ ๋ก๋ฉ ํ์๊ธฐ)์ผ ์ ์์ต๋๋ค.
- ๋คํธ์ํฌ ๋๊ธฐ ์๊ฐ ๊ณ ๋ ค: ๋คํธ์ํฌ ๋๊ธฐ ์๊ฐ์ ์ผ๋์ ๋์ญ์์ค. ๋๊ธฐ ์๊ฐ์ด ์ง์์ ์ผ๋ก ๋์ผ๋ฉด ๋๊ด์ ์ ๋ฐ์ดํธ๊ฐ ํจ๊ณผ์ ์ด์ง ์์ ์ ์์ต๋๋ค. ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ์ ๊ฐ์ ๋์ฒด ์ ๋ต์ ๊ณ ๋ คํ์ญ์์ค.
- ์ ์ ํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์ฌ์ฉ: ์ ๋ฐ์ดํธ ๋ฐ ๋๋๋ฆฌ๊ธฐ์ ํจ์จ์ ์ธ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ํํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ ์ํ๋ก ๋๋๋ฆฌ๋ ํ๋ก์ธ์ค๋ฅผ ๋จ์ํํ ์ ์์ต๋๋ค.
- ์ ๋ฐ์ดํธ ์ง์ญํ: ์์ ์ ์ํฅ์ ๋ฐ๋ ํน์ UI ์์์๋ง ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์ ์ฉํฉ๋๋ค. ๋ถํ์ํ๊ฒ ์ ์ฒด UI๋ฅผ ์ ๋ฐ์ดํธํ์ง ๋ง์ญ์์ค.
- ์์ง ์ผ์ด์ค ๊ณ ๋ ค: ๋์ ์ ๋ฐ์ดํธ ๋๋ ์ถฉ๋ ๋ฐ์ดํฐ์ ๊ฐ์ ์ ์ฌ์ ์ธ ์์ง ์ผ์ด์ค์ ๋ํด ์๊ฐํฉ๋๋ค. ์ด๋ฌํ ์ํฉ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ ์ ํ ์ ๋ต์ ๊ตฌํํฉ๋๋ค.
- ์ฌ์ฉ์ ์ ๋ ฅ ๋๋ฐ์ด์ค ๋๋ ์ค๋กํ: ์ฌ์ฉ์๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ์ ๋ ฅํ๋ ์๋๋ฆฌ์ค(์: ๊ฒ์ ์์์ ์ ๋ ฅ)์์๋ ๋๋ฐ์ด์ฑ ๋๋ ์ค๋กํ๋ง๊ณผ ๊ฐ์ ๊ธฐ์ ์ ์ฌ์ฉํ์ฌ ๋๊ด์ ์ ๋ฐ์ดํธ ๋น๋๋ฅผ ์ ํํ๊ณ ์๋ฒ์ ๊ณผ๋ถํ๊ฐ ๊ฑธ๋ฆฌ์ง ์๋๋ก ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
- ์บ์ฑ๊ณผ ํจ๊ป ์ฌ์ฉ: ์บ์ฑ ๋ฉ์ปค๋์ฆ๊ณผ ํจ๊ป ๋๊ด์ ์ ๋ฐ์ดํธ๋ ์ํํ ๊ฒฝํ์ ์ ๊ณตํ ์ ์์ต๋๋ค. UI์ ํจ๊ป ์บ์๋ฅผ ๋๊ด์ ์ผ๋ก ์ ๋ฐ์ดํธํ๊ณ ์๋ฒ ๋ฐ์ดํฐ๊ฐ ๋์ฐฉํ๋ฉด ์กฐ์ ํฉ๋๋ค.
- ๊ณผ๋ํ ์ฌ์ฉ ๋ฐฉ์ง: ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์ ๋ต์ ์ผ๋ก ์ฌ์ฉํฉ๋๋ค. ๊ณผ๋ํ๊ฒ ์ฌ์ฉํ๋ฉด ์ ๋ฐ์ดํธ๊ฐ ์์ฃผ ์คํจํ ๊ฒฝ์ฐ ํผ๋์ ์ผ์ผํฌ ์ ์์ต๋๋ค. ์ธ์ง๋ ์๋ต์ฑ์ด ์ค์ํ ์ํธ ์์ฉ์ ์ง์คํ์ญ์์ค.
useOptimistic
์ ๋ํ ์ ์ญ์ ๊ณ ๋ ค ์ฌํญ
์ ์ธ๊ณ ์ฌ์ฉ์๋ฅผ ์ํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ ๋๋ ๋ค์๊ณผ ๊ฐ์ ์์๋ฅผ ๊ณ ๋ คํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
- ๋คํธ์ํฌ ์กฐ๊ฑด: ๋คํธ์ํฌ ์กฐ๊ฑด์ ์ง์ญ์ ๋ฐ๋ผ ํฌ๊ฒ ๋ค๋ฅผ ์ ์์ต๋๋ค. ๋๊ด์ ์ ๋ฐ์ดํธ๋ ์ ๋ขฐํ ์ ์๊ฑฐ๋ ๋๋ฆฐ ์ธํฐ๋ท ์ฐ๊ฒฐ์ด ์๋ ์ง์ญ์์ ํนํ ์ ์ฉํ ์ ์์ต๋๋ค.
- ์ง์ญํ: ์ค๋ฅ ๋ฉ์์ง ๋ฐ ๊ธฐํ UI ์์๊ฐ ๋ค์ํ ์ธ์ด ๋ฐ ์ง์ญ์ ๋ง๊ฒ ์ ์ ํ๊ฒ ์ง์ญํ๋์๋์ง ํ์ธํฉ๋๋ค.
- ์ ๊ทผ์ฑ: ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ฅ์ ๊ฐ ์๋ ์ฌ์ฉ์๊ฐ ์ ๊ทผํ ์ ์๋์ง ํ์ธํฉ๋๋ค. ๋๊ด์ ์ ๋ฐ์ดํธ๊ฐ ๋ณด์กฐ ๊ธฐ์ ๊ณผ ํธํ๋์ง ์๋ ๊ฒฝ์ฐ UI์ ์ํธ ์์ฉํ ์ ์๋ ๋์ฒด ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
- ๋ฐ์ดํฐ ์ฃผ๊ถ: ๋ค์ํ ๊ตญ๊ฐ์ ๋ฐ์ดํฐ ์ฃผ๊ถ ๊ท์ ์ ์ผ๋์ ๋์ญ์์ค. ๋ฐ์ดํฐ๊ฐ ํ์ง ๋ฒ๋ฅ ์ ์ค์ํ์ฌ ์ฒ๋ฆฌ ๋ฐ ์ ์ฅ๋๋์ง ํ์ธํฉ๋๋ค.
- ์๊ฐ๋: ๋ ์ง์ ์๊ฐ์ ํ์ํ ๋ ์๊ฐ๋๋ฅผ ๊ณ ๋ คํ์ญ์์ค. ๋๊ด์ ์ ๋ฐ์ดํธ์๋ ์ฌ์ฉ์ ์์น์ ๋ํด ํ์๋ ์ ๋ณด๊ฐ ์ ํํ๋๋ก ์กฐ์ ์ด ํ์ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ฝ์์ด ๋๊ด์ ์ผ๋ก ์์ฑ๋ ๊ฒฝ์ฐ ์๋ฆผ์ด ์ฌ์ฉ์ ์๊ฐ๋์ ํ์๋๋์ง ํ์ธํฉ๋๋ค.
useOptimistic
์ ๋ํ ๋์
useOptimistic
์ ๋๊ด์ ์
๋ฐ์ดํธ๋ฅผ ๊ตฌํํ๋ ํธ๋ฆฌํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ์ง๋ง ๋์์ ์ธ ์ ๊ทผ ๋ฐฉ์์ด ์์ต๋๋ค.
- ์๋ ์ํ ๊ด๋ฆฌ: React์
useState
๋ฐuseEffect
ํ ์ ์ฌ์ฉํ์ฌ ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ์๋์ผ๋ก ๊ตฌํํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์ ๋ฐ์ดํธ ํ๋ก์ธ์ค๋ฅผ ๋ ๋ง์ด ์ ์ดํ ์ ์์ง๋ง ๋ ๋ง์ ์ฝ๋๊ฐ ํ์ํฉ๋๋ค. - ์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ: Redux ๋๋ Zustand์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ณ ๋๊ด์ ์ ๋ฐ์ดํธ๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค. ์ด๋ฌํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ณต์กํ ์ํ ์ ํ์ ๊ด๋ฆฌํ๊ธฐ ์ํ ๊ณ ๊ธ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- GraphQL ๋ผ์ด๋ธ๋ฌ๋ฆฌ: Apollo Client ๋ฐ Relay์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ GraphQL API๋ก ์์ ํ ๋ ๋๊ด์ ์ ๋ฐ์ดํธ์ ๋ํ ๊ธฐ๋ณธ ์ ๊ณต ์ง์์ ์ ๊ณตํฉ๋๋ค.
๊ฒฐ๋ก
React์ useOptimistic
ํ
์ ๋ณด๋ค ๋ฐ์์ฑ์ด ์ข๊ณ ๋งค๋ ฅ์ ์ธ ์ฌ์ฉ์ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋๋ ๊ฐ๋ ฅํ ๋๊ตฌ์
๋๋ค. ๋๊ด์ ์
๋ฐ์ดํธ์ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ๋ฐ๋ฅด๋ฉด React ์ ํ๋ฆฌ์ผ์ด์
์ ์ฌ์ฉ์ ๊ฒฝํ์ ํฌ๊ฒ ํฅ์์ํฌ ์ ์์ต๋๋ค. ์์
๋ฏธ๋์ด ํ๋ซํผ, ์ ์ ์๊ฑฐ๋ ์น์ฌ์ดํธ ๋๋ ํ์
๋๊ตฌ๋ฅผ ๊ตฌ์ถํ๋ ๋๊ด์ ์
๋ฐ์ดํธ๋ ์ ์ธ๊ณ ์ฌ์ฉ์๋ฅผ ์ํด ๋ ๋ถ๋๋ฝ๊ณ ์ฆ๊ฑฐ์ด ๊ฒฝํ์ ๋ง๋๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค. ๋ค์ํ ์ฌ์ฉ์๋ฅผ ์ํด ๋๊ด์ ์
๋ฐ์ดํธ๋ฅผ ๊ตฌํํ ๋๋ ๋คํธ์ํฌ ์กฐ๊ฑด, ์ง์ญํ ๋ฐ ์ ๊ทผ์ฑ๊ณผ ๊ฐ์ ์ ์ญ์ ์์๋ฅผ ๊ณ ๋ คํ๋ ๊ฒ์ ์์ง ๋ง์ญ์์ค.