React 服務(wù)器功能基礎(chǔ)知識(shí)
- 服務(wù)器組件(React Server Component,RSC):運(yùn)行在服務(wù)器上并像SSR一樣渲染到客戶端的組件。
- 服務(wù)器功能(又名服務(wù)器操作):僅在服務(wù)器上運(yùn)行并將信息發(fā)送到客戶端的功能。
指令
- 'use client':如果您使用的是啟用“服務(wù)器組件”的環(huán)境,則應(yīng)在文件頂部標(biāo)記 use client 以將其標(biāo)記為客戶端組件。
- ‘use server’:如果你考慮改變服務(wù)器上的信息,你應(yīng)該在服務(wù)器組件或文件的函數(shù)體頂部標(biāo)記 use server 指令,以僅在服務(wù)器上操作。
服務(wù)器組件
- RSC 沒有狀態(tài),也沒有生命周期。您不能在服務(wù)器組件上使用任何鉤子函數(shù)(useState、useEffect 等)。
- RSC 可以有異步函數(shù)。
- RSC可以具有服務(wù)器功能。但您必須在函數(shù)體頂部標(biāo)記“使用服務(wù)器”。
- RSC 可以在服務(wù)器組件和客戶端組件中擁有子組件。
- RSC 無法運(yùn)行任何瀏覽器 API。
- 您的 RSC 可以將任何類型的 props 傳遞給 RSC。還可以將 props 類型傳遞給客戶端組件。檢查下面的指令類型表。
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>; }
服務(wù)器功能
- 定義時(shí),函數(shù)體或文件中必須有“use server”指令。
- 您可以在函數(shù)體上運(yùn)行任何服務(wù)器功能。
- 您無法在函數(shù)體上運(yùn)行瀏覽器API。
- 您可以在服務(wù)器和客戶端上調(diào)用服務(wù)器函數(shù)。
- 您可以返回有限制的值。檢查下面的指令類型表。
在服務(wù)器組件中聲明服務(wù)器函數(shù)
import Button from './Button'; function EmptyNote () { async function createNoteAction() { // Server Function 'use server'; await db.notes.create(); } return <Button onClick={createNoteAction}/>; }
在單獨(dú)的文件中聲明服務(wù)器功能
"use server"; export async function updateName(name) { if (!name) { return {error: 'Name is required'}; } await db.users.updateName(name); }
在客戶端組件中使用服務(wù)器功能
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>; }
指令中句柄值類型的比較
- 使用客戶端:將 prop 類型從服務(wù)器組件傳遞到客戶端組件。
- use server: Server函數(shù)的返回類型
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. |
- 其他類型和實(shí)例不可用。
快樂反應(yīng)!
以上是React 服務(wù)器功能備忘單的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)

javascriptisidealforwebdevelogment,whilejavasuitslarge-scaleapplicationsandandandroiddevelopment.1)javascriptexceleatingingingingingingingbeatingwebexperienceswebexperienceswebexperiencesandfull-stackdeevermentwithnode.js.2)

在JavaScript中,選擇單行注釋(//)還是多行注釋(//)取決于注釋的目的和項(xiàng)目需求:1.使用單行注釋進(jìn)行快速、內(nèi)聯(lián)的解釋;2.使用多行注釋進(jìn)行詳細(xì)的文檔說明;3.保持注釋風(fēng)格的一致性;4.避免過度注釋;5.確保注釋與代碼同步更新。選擇合適的注釋風(fēng)格有助于提高代碼的可讀性和可維護(hù)性。

是的,javascriptcommentsarenectary和shouldshouldshouldseffectional.1)他們通過codeLogicAndIntentsgudedepleders,2)asevitalincomplexprojects,和3)handhanceClaritywithOutClutteringClutteringThecode。

Java和JavaScript是不同的編程語言,各自適用于不同的應(yīng)用場(chǎng)景。Java用于大型企業(yè)和移動(dòng)應(yīng)用開發(fā),而JavaScript主要用于網(wǎng)頁開發(fā)。

JavascriptconcommentsenceenceEncorenceEnterential gransimenting,reading and guidingCodeeXecution.1)單inecommentsareusedforquickexplanations.2)多l(xiāng)inecommentsexplaincomplexlogicorprovideDocumentation.3)

評(píng)論arecrucialinjavascriptformaintainingclarityclarityandfosteringCollaboration.1)heelpindebugging,登機(jī),andOnderStandingCodeeVolution.2)使用林格forquickexexplanations andmentmentsmmentsmmentsmments andmmentsfordeffordEffordEffordEffordEffordEffordEffordEffordEddeScriptions.3)bestcractices.3)bestcracticesincracticesinclud

JavaScripthasseveralprimitivedatatypes:Number,String,Boolean,Undefined,Null,Symbol,andBigInt,andnon-primitivetypeslikeObjectandArray.Understandingtheseiscrucialforwritingefficient,bug-freecode:1)Numberusesa64-bitformat,leadingtofloating-pointissuesli

JavaScriptIspreferredforredforwebdevelverment,而Javaisbetterforlarge-ScalebackendsystystemsandSandAndRoidApps.1)JavascriptexcelcelsincreatingInteractiveWebexperienceswebexperienceswithitswithitsdynamicnnamicnnamicnnamicnnamicnemicnemicnemicnemicnemicnemicnemicnemicnddommanipulation.2)
