Rumah >hujung hadapan web >tutorial js >Helaian cheat ciri pelayan React
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>; }
Mengisytiharkan fungsi pelayan dalam komponen pelayan
import Button from './Button'; function EmptyNote () { async function createNoteAction() { // Server Function 'use server'; await db.notes.create(); } return <Button onClick={createNoteAction}/>; }
Mengisytiharkan fungsi pelayan dalam fail berasingan
"use server"; export async function updateName(name) { if (!name) { return {error: 'Name is required'}; } await db.users.updateName(name); }
Menggunakan fungsi pelayan dalam komponen klien
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. |
Selamat bertindak!
Atas ialah kandungan terperinci Helaian cheat ciri pelayan React. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!