Some of the data that the JavaScript SDK returns, such as blog posts and database collection items, contains rich content. Rich content is enhanced text content that can include things like formatted text, link previews, images, and video.
The SDK returns rich content as complex objects. If you’re creating a Headless project with a custom frontend, it can be challenging to render the objects yourself. If you build your frontend using React, you can use the ricos-viewer
package from Ricos.js to render rich content objects returned by the SDK. Ricos is an open source product designed for editing and rendering rich content objects. It supports Wix rich content objects.
This tutorial explains how to use Ricos to render the rich content objects returned by the SDK. It includes examples for both the Blog and Data modules.
This process has three steps:
Before getting started, you need:
The first step in rendering rich content is to retrieve it in your frontend code using the SDK. To do this:
In the terminal, run the following commands to install the necessary SDK packages:
yarn add @wix/sdk
yarn add @wix/blog
yarn add @wix/data
Create a new file in your project called rich-content-api.js
.
At the top of your file, add the following import statements.
Create an SDK client by adding the following code to your code file. Fill in the value for your client ID. Adjust the value of modules
if you are only working with Blog or CMS:
Add a function to your file called getRichContent
. This function retrieves the rich content data from your project.
Use the SDK Data module’s queryDataItems function to retrieve your project’s CMS items. You can use the query builder to refine your results.
Example code:
Use the SDK Blog module’s queryPosts function to retrieve your project’s blog posts. You can use the query builder to refine your results.
Example code:
The next step is to create a React component that uses the ricos-viewer
package to display rich content objects. The viewer package doesn’t support all the Wix rich content types out of the box. However, Ricos provides plugins for them. Learn more about the plugins in the Ricos documentation.
The viewer generates a React class component with the rendered rich content. This means you can only work with this component in client code. If your project supports React Server Components, make sure to add ’use client’
at the top of your code file.
To create your component, do the following:
Install the ricos-viewer
package, its dependencies, and the plugins to support the different Wix rich content types. To do this, run the commands below.
The commands install the plugins for all the rich content types. If there are some types that you aren’t using in your project, you can leave them out.
Note: You must use the same version for ricos-viewer
and the plugin packages. We recommend major version 9
.
Create a new file in your project called RichContentViewer.jsx
.
Add ’use client’
to the top of the file if needed. See the introduction to this section for details.
Import React
, the viewer package, and all the plugins.
Create an array of initialized plugin objects. You can modify the way different rich content types are rendered by passing configuration objects to the plugin functions. For more information, see the Ricos documentation.
Create a React component called RichContentViewer
that accepts content
as a prop and returns a RicosViewer
component with content
and the plugins
array from the previous step passed in as props. Export the component.
The final step is to create a React component that uses the code from the previous sections to retrieve rich content and display it on a page. If you’re working in a framework like Next.js, this component would probably be a React Server Component defined in the code file for a particular page on your site.
To create this component, do the following:
Add the following import statements to your file. Make sure to include the correct paths to your rich-content-api
and RichContentViewer
files.
toDraft()
converts the rich content object received from the SDK into Draft.js format that the Ricos viewer uses.
Create a component called RichContent
that uses your getRichContent
function to retrieve your content, converts to Draft.js format using toDraft()
, and then passes the result to your RichContentViewer
component.
Export another component called Page
that includes your RichContent
component. In a real site, this component would likely include other elements of your page.
You now have a basic app that can render rich content using React. For more information about working with the SDK, see the SDK Reference.