What changes for RSC
Starting from v4.2 React Bricks supports React Server Components (RSC) with the react-bricks/rsc
package.
If you would like to know more about RSC, we suggest to read the Where do React Server Components fit in the history of web development article by Matteo Frana and also Making Sense of React Server Components by Josh Comeau.
As Next.js is the first framework leveraging React Server Components, RSC are now available in the Next.js starters with the App Router. If you scaffold a new project from the CLI using the “Next.js with App Router” starter, you will have a complete project using React Bricks in the React Server Components flavour.
Server components are a completely new paradigm and this imposed some changes to the React Bricks APIs in the react-bricks/rsc
package.
In this section of the documentation, you will find everything that changes when using Server Components with an RSC-enabled starter and the react-bricks/rsc
package.
New packages: /rsc
and /rsc/client
Server components
In Server Components, the following items should be imported from react-bricks/rsc
:
File
, Image
, JsonLd
, Link
, PageViewer
, Plain
, Repeater
, RichText
, RichTextExt
, Text
, blockPluginConstructor
, blockWithModalPluginConstructor
, cleanPage
, fetchPage
, fetchPages
, fetchTags
, getPagePlainText
, markPluginConstructor
, plugins
, types
While the following imports are not available for Server Components:
Preview
, Meta
, getSchemaOrgData
, renderJsonLd
, renderMeta
, useAdminContext
, usePage
, usePagePublic
, usePageValues
, usePages
, usePagesPublic
, useReactBricksContext
, useTagsPublic
, useVisualEdit
,
Client components
In Client Components (with ‘use client’), the following items should be imported from react-bricks/rsc/client
:
ReactBricks
, useAdminContext
, usePageValues
, useReactBricksContext
, useVisualEdit
, types
Hooks replacements for server components
isAdmin
Function which returns true
if execution runs inside the Admin interface, false
otherwise.
It replaces the useAdminContext
hook, not available for server components.
getPageValues
Function which returns the pageValues of the current page.
It replaces the usePageValues
hook, not available for server components.
For client components
register
Function that initializes the React Bricks configuration so that it is available to render the page with RSC.
This function should be invoked before using any component or function imported from react-bricks/rsc
.
wrapClientComponent
Function which wraps a client component (a component with 'use client'
) and, adding the schema
, returns a React Bricks’ brick.
RegisterComponent is a function from react-bricks/rsc/client
.
Meta data
getMetadata
Next.js 14+ with the App Router has its way of managing meta data. This function returns the page metadata so that they are compatible with Next.js Metadata.
Preview link
fetchPagePreview
Function which fetches the page data so that it can be viewed in a preview link.
It is an alternative solution to the <Preview>
component used in the non-RSC library.
Other functions
getBlockValue
Function which returns the value of the propName for the current block in a server component.
getBricks
Function to get all the configuration’s bricks in a server component.
Under react-bricks/rsc/client
RegisterComponent
Function to be used together with wrapClientComponent
to create bricks from client components.
ClickToEdit
Client component that shows the icon to edit the page when a user is viewing the frontend website while logged in the admin interface.
Updated Components
When using React Server Components, the visual components (Text, RichText, Image, File Repeater, Link) must be imported from react-bricks/rsc
and they have a slightly different APIs, as we need to pass them the value of the prop.
Text
Add the value
prop. You get it from the component’s props.
The type should be declared as ReactBricks.types.TextValue
.
Before
After
RichText
Add the value
prop. You get it from the component’s props.
The type should be declared as ReactBricks.types.TextValue
.
Before
After
Image
Add the source
prop. You get it from the component’s props.
The type should be declared as ReactBricks.types.IImageSource
.
Before
After
File
Add the source
prop. You get it from the component’s props.
The type should be declared as ReactBricks.types.IFileSource
.
Before
After
Repeater
Add the items
prop. You get it from the component’s props.
The type should be declared as ReactBricks.types.RepeaterItems<T>
where T (not mandatory) is the type of the single repeated item (you can import it from the repeated brick).
If you specify T, you have the result of getDefaultProps
fully typed also for repeated items.