import marked from 'marked'; // Not included in bundle import sanitizeHtml from 'sanitize-html'; // Not included in bundle async function Page({page}) { // NOTE: loads *during* render, when the app is built. const content = await file.readFile(`${page}.md`); return <div>{sanitizeHtml(marked(content))}</div>; }
서버 컴포넌트에서 서버 기능 선언
import Button from './Button'; function EmptyNote () { async function createNoteAction() { // Server Function 'use server'; await db.notes.create(); } return <Button onClick={createNoteAction}/>; }
별도의 파일에 서버 기능 선언
"use server"; export async function updateName(name) { if (!name) { return {error: 'Name is required'}; } await db.users.updateName(name); }
클라이언트 컴포넌트의 서버 기능 사용
import marked from 'marked'; // Not included in bundle import sanitizeHtml from 'sanitize-html'; // Not included in bundle async function Page({page}) { // NOTE: loads *during* render, when the app is built. const content = await file.readFile(`${page}.md`); return <div>{sanitizeHtml(marked(content))}</div>; }
Type | use client | use server | Notes |
---|---|---|---|
string | ✅ | ✅ | both string value and iterables are supported. |
number | ✅ | ✅ | |
bigint | ✅ | ✅ | |
boolean | ✅ | ✅ | |
undefined | ✅ | ✅ | |
null | ✅ | ✅ | |
Array | ✅ | ✅ | Only available in the item of serializable list. |
Map | ✅ | ✅ | Only available in the item of serializable list. |
Set | ✅ | ✅ | Only available in the item of serializable list. |
TypedArray | ✅ | ✅ | |
ArrayBuffer | ✅ | ✅ | |
Date | ✅ | ✅ | |
object | ✅ | ✅ | Support only plain object(object initializers or JSON), null prototype not supported. |
Promises | ✅ | ✅ | Only available in the serializable list. |
ReactNode | ✅ | ❌ | Only Server Component can send it to Client Component via props. |
FormData | ❌ | ✅ | Only server functions can return FormData instance. |
symbol | ⚠️ | ⚠️ | Only symbols registered in the global Symbol registry via Symbol.for |
function | ⚠️ | ⚠️ | Only server functions allowed. |
class | ❌ | ❌ | Any instance objects are not serializable. |
즐거운 리액션!
위 내용은 React 서버 기능 치트 시트의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!