Blog.

What is BaseHub?

Cover Image for What is BaseHub?
Julian Benegas
Julian Benegas

BaseHub is a headless CMS that helps you write, store and distribute content for the Internet, faster. In our docs, we aim to give you an overview of BaseHub’s core concepts, as well as guide you through common use cases of the platform.

Fast, Collaborative, AI-Native Content Management
Fast, Collaborative, AI-Native Content Management

How to use Rich Text in React?

The Delivery API can return your Rich Text Blocks’ data in multiple formats:

  1. Plain Text, will ignore all formatting, media, and custom components, easy to render.

  2. HTML, will ignore custom components, easy to render.

  3. Markdown, will ignore custom components, needs a markdown to HTML parser to render.

  4. JSON, comes with everything, but needs something that understand and processes it.

In the case of the JSON format, the response will be an AST based on the TipTap editor spec. Because of the complexities associated with processing this JSON format, we’ve built a React Component called <RichText /> that will help us render our Rich Text content. This is how it works:

import { Pump } from "basehub/react-pump"
import { RichText } from "basehub/react-rich-text"

const Page = async () => {
  return (
    <Pump
      draft={draftMode().isEnabled}
      next={{ revalidate: 60 }}
      queries={[
        {
          homepage: {
            subtitle: { 
              json: { 
                content: true, 
              }, 
            }, 
          },
        },
      ]}
    >
      {async ([{ homepage }]) => {
        "use server"
        return <RichText>{homepage.subtitle.json.content}</RichText>
      }}
    </Pump>
  )
}

export default Page

When using the <RichText /> component, you can simply pass the JSON content into it via children, and it’ll get rendered.

How to use Fast Refresh with Pump? (Next.js Only)

Pump is a React Server Component that enables a Fast Refresh-like experience for your content. When set up, Pump will subscribe to real time changes from your Repo, and so keep your UI up-to-date. This is ideal for previewing content before pushing it to production. You can use it like this:

import { Pump } from "basehub/react-pump"
import { draftMode } from "next/headers"

const Page = () => {
  return (
    <Pump queries={[{ _sys: { id: true } }]} draft={draftMode().isEnabled}>
      {async ([data]) => {
        "use server" // needs to be a Server Action

        // some notes
        // 1. `data` is typesafe.
        // 2. if draft === true, this will run every time you edit the content, in real time.
        // 3. you can render nested Server Components (Client or Server) here, go nuts.

        return (
          <pre>
            <code>{JSON.stringify(data, null, 2)}</code>
          </pre>
        )
      }}
    </Pump>
  )
}

export default Page

Note: This is a fragment of the BaseHub documentation. For more info, please go to our official docs.


More Posts

Cover Image for About Learning Docker

About Learning Docker

A succinct overview of the key benefits and features of Docker for those looking to streamline their development process.

Lee Robinson
Lee Robinson
Cover Image for Tweaking My Avatar

Tweaking My Avatar

Boost your avatar's uniqueness with creative adjustments! 🎨

Evil Rabbit
Evil Rabbit
Cover Image for Making the Web. Faster.

Making the Web. Faster.

A compelling overview detailing the methods and strategies employed to optimize website performance and enhance user experience.

Guillermo Rauch
Guillermo Rauch