React์˜ use: ๋ฆฌ์†Œ์Šค ํ›…: ์ข…ํ•ฉ ๊ฐ€์ด๋“œ | MLOG | MLOG}> {/* Assuming this ID doesn't exist and will cause an error */} ); } export default App;

์ด ์˜ˆ์ œ์—์„œ fetchUser ํ•จ์ˆ˜๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด(์˜ˆ: 404 ์ƒํƒœ ์ฝ”๋“œ), ErrorBoundary ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ํฌ์ฐฉํ•˜์—ฌ ํด๋ฐฑ UI๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ํด๋ฐฑ์€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋‚˜ ์žฌ์‹œ๋„ ๋ฒ„ํŠผ๊ณผ ๊ฐ™์€ ๋ชจ๋“  React ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

use: ๊ณ ๊ธ‰ ๊ธฐ์ˆ 

1. ๋ฆฌ์†Œ์Šค ์บ์‹ฑ

์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌ์†Œ์Šค(ํ”„๋กœ๋ฏธ์Šค)๋ฅผ ์บ์‹œํ•˜๊ณ  ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ๋‚˜ ๋ Œ๋”๋ง์— ๊ฑธ์ณ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ตœ์ ํ™”๋Š” ์„ฑ๋Šฅ์— ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

            
import React, { Suspense, useRef } from 'react';

const resourceCache = new Map();

async function fetchUser(id) {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
  if (!response.ok) {
    throw new Error(`Failed to fetch user: ${response.status}`);
  }
  return response.json();
}

function getUserResource(userId) {
  if (!resourceCache.has(userId)) {
    resourceCache.set(userId, {
      read() {
        if (!this.promise) {
          this.promise = fetchUser(userId);
        }
        if (this.result) {
          return this.result;
        }
        throw this.promise;
      }
    });
  }
  return resourceCache.get(userId);
}

function UserProfile({ userId }) {
  const resource = getUserResource(userId);
  const user = resource.read();

  return (
    

{user.name}

Email: {user.email}

Phone: {user.phone}

); } function App() { return ( Loading user data...
}> ); } export default App;

์ด ์˜ˆ์ œ์—์„œ:

2. ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ์™€ ํ•จ๊ป˜ use: ์‚ฌ์šฉํ•˜๊ธฐ

use: ํ›…์€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์„œ๋ฒ„์—์„œ ์ง์ ‘ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” React ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์ดˆ๊ธฐ ํŽ˜์ด์ง€ ๋กœ๋“œ ์†๋„๋ฅผ ๋†’์ด๊ณ  SEO๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๊ฒฐ๊ณผ๋กœ ์ด์–ด์ง‘๋‹ˆ๋‹ค.

Next.js ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ ์˜ˆ์ œ

            
// app/user/[id]/page.jsx (Server Component in Next.js)
import React from 'react';

async function fetchUser(id) {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
  if (!response.ok) {
    throw new Error(`Failed to fetch user: ${response.status}`);
  }
  return response.json();
}

export default async function UserPage({ params }) {
  const user = React.use(fetchUser(params.id));

  return (
    

{user.name}

Email: {user.email}

Phone: {user.phone}

); }

์ด Next.js ์„œ๋ฒ„ ์ปดํฌ๋„ŒํŠธ์—์„œ fetchUser ํ•จ์ˆ˜๋Š” ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. use: ํ›…์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์งˆ ๋•Œ๊นŒ์ง€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ผ์‹œ ์ค‘๋‹จ์‹œ์ผœ ํšจ์œจ์ ์ธ ์„œ๋ฒ„์‚ฌ์ด๋“œ ๋ Œ๋”๋ง์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

use:๋ฅผ ์œ„ํ•œ ๋ชจ๋ฒ” ์‚ฌ๋ก€

์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์ œ

1. ์ด์ปค๋จธ์Šค ์ƒํ’ˆ ๋ชฉ๋ก

์ƒํ’ˆ ๋ชฉ๋ก์„ ํ‘œ์‹œํ•˜๋Š” ์ด์ปค๋จธ์Šค ์›น์‚ฌ์ดํŠธ๋ฅผ ์ƒ์ƒํ•ด ๋ณด์„ธ์š”. ๊ฐ ์ƒํ’ˆ ์นด๋“œ๋Š” use:๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ’ˆ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

            
// ProductCard.jsx
import React, { Suspense } from 'react';

async function fetchProduct(productId) {
  const response = await fetch(`/api/products/${productId}`);
  if (!response.ok) {
    throw new Error(`Failed to fetch product: ${response.status}`);
  }
  return response.json();
}

function ProductCard({ productId }) {
  const product = React.use(fetchProduct(productId));

  return (
    

{product.name}

{product.description}

Price: ${product.price}

); } function ProductList({ productIds }) { return (
{productIds.map((productId) => ( Loading product...
}> ))}
); } export default ProductList;

์ด ์ ‘๊ทผ ๋ฐฉ์‹์€ ๊ฐ ์ƒํ’ˆ ์นด๋“œ๊ฐ€ ๋…๋ฆฝ์ ์œผ๋กœ ๋กœ๋“œ๋˜๋„๋ก ๋ณด์žฅํ•˜๋ฉฐ, ๋А๋ฆฌ๊ฒŒ ๋กœ๋“œ๋˜๋Š” ์ƒํ’ˆ์œผ๋กœ ์ธํ•ด ์ „์ฒด ํŽ˜์ด์ง€ ๋ Œ๋”๋ง์ด ์ฐจ๋‹จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๊ฐ ์ƒํ’ˆ์— ๋Œ€ํ•œ ๊ฐœ๋ณ„ ๋กœ๋”ฉ ํ‘œ์‹œ๊ธฐ๋ฅผ ๋ณด๊ฒŒ ๋˜์–ด ๋” ๋‚˜์€ ๊ฒฝํ—˜์„ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค.

2. ์†Œ์…œ ๋ฏธ๋””์–ด ํ”ผ๋“œ

์†Œ์…œ ๋ฏธ๋””์–ด ํ”ผ๋“œ๋Š” use:๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ํ”„๋กœํ•„, ๊ฒŒ์‹œ๋ฌผ, ๋Œ“๊ธ€์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

            
// Post.jsx
import React, { Suspense } from 'react';

async function fetchPost(postId) {
  const response = await fetch(`/api/posts/${postId}`);
  if (!response.ok) {
    throw new Error(`Failed to fetch post: ${response.status}`);
  }
  return response.json();
}

async function fetchComments(postId) {
  const response = await fetch(`/api/posts/${postId}/comments`);
  if (!response.ok) {
    throw new Error(`Failed to fetch comments: ${response.status}`);
  }
  return response.json();
}

function Comments({ postId }) {
  const comments = React.use(fetchComments(postId));

  return (
    
    {comments.map((comment) => (
  • {comment.text}
  • ))}
); } function Post({ postId }) { const post = React.use(fetchPost(postId)); return (

{post.title}

{post.content}

Loading comments...
}>
); } export default Post;

์ด ์˜ˆ์ œ๋Š” ์ค‘์ฒฉ๋œ Suspense ๊ฒฝ๊ณ„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒŒ์‹œ๋ฌผ ๋‚ด์šฉ๊ณผ ๋Œ“๊ธ€์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๋Œ“๊ธ€์ด ๋กœ๋“œ๋˜๋Š” ๋™์•ˆ์—๋„ ๊ฒŒ์‹œ๋ฌผ ๋‚ด์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”ํ•œ ํ•จ์ •๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

use:์˜ ๋Œ€์•ˆ

use:๊ฐ€ ์ƒ๋‹นํ•œ ์ด์ ์„ ์ œ๊ณตํ•˜์ง€๋งŒ, React์—์„œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

์ด๋Ÿฌํ•œ ๋Œ€์•ˆ๋“ค ์‚ฌ์ด์˜ ์„ ํƒ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณต์žก์„ฑ๊ณผ ํŠน์ • ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ ์‹œ๋‚˜๋ฆฌ์˜ค์˜ ๊ฒฝ์šฐ use:๊ฐ€ ํ›Œ๋ฅญํ•œ ์˜ต์…˜์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋” ๋ณต์žกํ•œ ์‹œ๋‚˜๋ฆฌ์˜ค์˜ ๊ฒฝ์šฐ useSWR๋‚˜ React Query์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋” ์ ํ•ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

React์˜ use: ํ›…์€ ๋ฆฌ์†Œ์Šค ๋กœ๋”ฉ๊ณผ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ•๋ ฅํ•˜๊ณ  ์„ ์–ธ์ ์ธ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Suspense์™€ ํ•จ๊ป˜ use:๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ ๋กœ์ง์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ , ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๊ฐœ์„ ํ•˜๋ฉฐ, ์„ฑ๋Šฅ์„ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ use:๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ์‚ฌํ•ญ, ๊ณ ๊ธ‰ ๊ธฐ์ˆ  ๋ฐ ๋ชจ๋ฒ” ์‚ฌ๋ก€๋ฅผ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ง€์นจ์„ ๋”ฐ๋ฅด๋ฉด ๋น„๋™๊ธฐ ์ž‘์—…์„ ํšจ๊ณผ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ๊ฒฌ๊ณ ํ•˜๋ฉฐ ์„ฑ๋Šฅ์ด ๋›ฐ์–ด๋‚˜๊ณ  ์‚ฌ์šฉ์ž ์นœํ™”์ ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. React๊ฐ€ ๊ณ„์† ๋ฐœ์ „ํ•จ์— ๋”ฐ๋ผ, use:์™€ ๊ฐ™์€ ๊ธฐ์ˆ ์„ ๋งˆ์Šคํ„ฐํ•˜๋Š” ๊ฒƒ์€ ์•ž์„œ ๋‚˜๊ฐ€๊ณ  ํƒ์›”ํ•œ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ๋ฐ ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ  ์ž๋ฃŒ