packages/react-server)packages/react-client)react-server와 react-client 패키지는 React 저장소 내부에 있습니다.<p>Hello, world</p><p> 태그를 JSON으로 변환하려면 다음과 같이 할 수 있습니다:{ type: 'p', props: { children: 'Hello world' }}<Counter> 태그를 생각해봅시다. 어떻게 serialize할까요?import { Counter } from './client';<Counter initialCount={10} />'use client';import { useState, useEffect } from 'react';export function Counter({ initialCount }) { const [count, setCount] = useState(initialCount); // ...}<Counter>를 복원하고 싶다는 것을 기억하세요—따라서 우리는 단지 스냅샷을 원하는 것이 아닙니다. 우리는 상호작용을 위한 전체 로직을 원합니다!Counter 코드를 JSON에 직접 포함시키는 것입니다:{ type: ` import { useState, useEffect } from 'react'; export function Counter({ initialCount }) { const [count, setCount] = useState(initialCount); // ... } `, props: { initialCount: 10 }}eval로 보내고 싶지 않으며, 같은 컴포넌트의 코드를 여러 번 보내고 싶지 않습니다. 대신 그 코드가 우리 앱에 의해 정적 JS 자산으로 제공되고 있다고 가정하는 것이 합리적입니다—이를 JSON에서 참조할 수 있습니다. 거의 <script> 태그 같습니다:{ type: '/src/client.js#Counter', // "Load src/client.js and grab Counter" props: { initialCount: 10 }}<script> 태그를 생성하여 로드할 수 있습니다.'use client'가 있는 파일을 찾고 실제로 해당 진입점에 대한 번들 청크를 생성하는 것입니다—Astro Islands와 조금 비슷합니다.'chunk123.js#Counter'와 같은 모듈을 참조할 수 있습니다.import { serialize } from 'react-server-dom-yourbundler'; // Bundler-specific packageconst reactTree = <Counter initialCount={10} />;const outputString = serialize(reactTree); // Something like the JSON aboveoutputString을 디스크에 저장하거나, 네트워크를 통해 보내거나, 캐시하거나, 무엇이든 할 수 있습니다—그리고 결국 React Client에 공급합니다. React Client는 전체 트리를 deserialize하고, 필요에 따라 참조된 모듈에서 코드를 로드합니다:import { deserialize } from 'react-server-dom-yourbundler/client'; // Bundler-specific packageconst outputString = // ... received over network, read from disk, etc...const reactTree = deserialize(outputString); // <Counter initialCount={10} /><Counter initialCount={10} />을 작성한 것처럼 일반적인 JSX 조각을 줄 것입니다. 당신은 그 트리로 무엇이든 할 수 있습니다—렌더링하고, 상태에 유지하고, HTML로 변환하는 등:const outputString = // ... received over network, read from disk, etc...const reactTree = deserialize(outputString); // <Counter initialCount={10} />// You can do anything you'd do with a regular JSX tree, for example:const root = createRoot(domNode);root.render(reactTree);serialize와 deserialize 이름은 설명적입니다. 정확한 이름은 바인딩에 따라 다르며 여러 오버로드가 있을 수 있습니다. 예를 들어, 기본 react-server-dom-parcel 바인딩에 대한 얇은 래퍼인 @parcel/rsc 패키지는 serialization을 renderRSC로, deserialization을 fetchRSC로 노출합니다. 또한 실제 구현은 non-blocking이며 양쪽에서 streaming을 지원합니다.)아직 댓글이 없습니다.
첫 번째 댓글을 작성해보세요!

"Bug-O" 표기법
Inkyu Oh • Front-End

RSC에서 Import가 작동하는 방식
Inkyu Oh • Front-End

Lean 문법 입문서
Inkyu Oh • AI & ML-ops

Boolean을 넘어서
Inkyu Oh • Front-End