React Server Components overview
Hydrogen is modelled after React Server Components, an approach that offers an opinionated data-fetching and rendering workflow for React apps.
This guide provides information about how React Server Components work in the context of Hydrogen.
How React Server Components work
React Server Components allow the server and the client to work together to render your Hydrogen app.
For example, the following React element tree is composed of React components that render other React components. React Server Components allow some components to render on the server, some to render on the client, and others to render on both the server and the client:
React Server Components include the following component types:
|Server||Components that fetch data and render content on the server. Their dependencies aren't in the client bundle. Server components don't include any client-side interactivity. Only server components can make calls to the Storefront API.||Server components end in
|Client||Components that render on the client. Client components include client-side stateful interactivity.||Client components end in
|Shared||Components that render on both the server and the client.||Shared components don't end in either
React Server Components separate client and server logic. This separation provides the following benefits to Hydrogen apps:
- Server-only code that has no impact on bundle size and reduces bundle sizes
- Server-side access to custom and private server-side data sources
- Seamless integration and a well-defined protocol for server and client components
- Streaming rendering and progressive hydration
- Subtree and component-level updates that preserve client state
- Server and client code sharing, where appropriate
React Server Components have the following constraints on server and client components:
- Client components can’t access server-only features, like the filesystem, and can only import other client components.
- Server components can’t access client-only features, like state.
Due to these constraints, there are specific rules that you need to follow when building your Hydrogen app:
One of the key constraints of React Server Components is that you can't import and render server components from client components. However, you can compose React Server Components to take in props.
The following example shows how to pass a server component as a
children prop to a client component. The
OuterServerComponent can then instantiate both the client and server components. This is how you can have server components under client components in your React element tree.
When you send props to client components from a server component, make sure that the props are JSON-serializable. For example, functions or callbacks can't be passed as props.
The following prop would send successfully:
The following prop wouldn't send successfully:
Sharing code between server and client
In addition to server-specific and client-specific components, you can create components that work on both the server and the client. This allows logic to be shared across environments, as long as the components meet all the constraints of both the server and client components.
Although shared components have the most constraints, many components already obey these rules and can be used across the server and client without modification. For example, many components transform some props based on certain conditions, without using state or loading additional data. This is why shared components are the default and don’t have a dedicated file extension.