일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 오공완
- map()에는 key값이 필요
- 리스트랜더링
- 클래스 추가하기 #특정 url 클래스 추가 #사이트 접속시 클래스 추가 #오공완 #javascript
- React
- 리액트
- 오공완 #리액트 공부 #React
- Today
- Total
new_bird-hyun
React 서버 컴포넌트와 클라이언트 컴포넌트 혼합 사용하기 본문
components/ServerComponent.js
export default async function ServerComponent() {
const res = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const post = await res.json();
return (
<div>
<h1>서버 컴포넌트</h1>
<h2>{post.title}</h2>
<p>{post.body}</p>
</div>
);
}
components/ClientComponent.js
'use client';
import { useState } from 'react';
export default function ClientComponent() {
const [count, setCount] = useState(0);
return (
<div>
<h1>클라이언트 컴포넌트</h1>
<p>카운트: {count}</p>
<button onClick={() => setCount(count + 1)}>증가</button>
</div>
);
}
app/page.js
import ServerComponent from './components/ServerComponent';
import ClientComponent from './components/ClientComponent';
export default function HomePage() {
return (
<div>
<ServerComponent />
<ClientComponent />
</div>
);
}
- 서버 컴포넌트를 활용하면 클라이언트에서 API 요청을 최소화할 수 있음
- 클라이언트 컴포넌트와 혼합하여 사용자 인터랙션을 추가 가능
'코딩 공부' 카테고리의 다른 글
Next.js 서버 액션(Server Actions)으로 이미지 업로드 및 저장하기 (0) | 2025.02.24 |
---|---|
Next.js 서버 액션(Server Actions)과 상태 관리 최적화 (1) | 2025.02.16 |
React 18 서버 컴포넌트(Server Components) 개념과 활용법 (0) | 2025.02.03 |
서버 측에서 데이터를 점진적으로 불러오는 패턴 - React Streaming과 Suspense (0) | 2025.02.01 |
React에서 무한 스크롤과 가상화를 결합하여 성능 최적화하기 (0) | 2025.01.31 |