Content rendering

Learn how to fetch and render content for your slots.

This guide provides practical examples of how to use the React SDK to fetch and render a  Slot in your application.

Basic usage

To fetch content, you can use either the useContent hook or the <Slot> component, depending on whether you prefer an imperative or declarative approach.

Croct's mascot amazed
I can generate code for you!

Go to the  Integration page , select the slot you want to implement, and get a fully functional integration code to copy and paste.

Here is an example:

components/HomeHero.jsx
12345678910111213
import {useContent} from '@croct/plug-react';
export function HomeHero() {
const content = useContent('home-hero');
return (
<div>
<strong>{content.title}</strong>
<p>{content.subtitle}</p>
<a href={content.button.link}>{content.button.label}</a>
</div>
);
}

If you are using a server-side rendering framework, you should also provide an initial content for pre-rendering, which will then be personalized on the client.

Since the content is fetched asynchronously, running the above code would cause the application to suspend. To render a loading state, you can use either a <Suspense> boundary or an initial value.

  • Wrap the component in a <Suspense> boundary to handle the load state:

    src/pages/HomePage.jsx
    12345678910
    import {Suspense} from 'react';
    import {HomeHero} from '../components/HomeHero';
    export default function HomePage() {
    return (
    <Suspense fallback="✨ Personalizing content...">
    <HomeHero />
    </Suspense>
    );
    }

For more information about the available options, refer to the documentation of the useContent hook and <Slot> component.

Fault tolerance

You should always provide a fallback content to make your application resilient to unexpected errors, downtime, and network failures.

All you have to do is specify the content you want to use as a fallback:

components/HomeHero.jsx
12345678910111213141516171819202122
import {useContent} from '@croct/plug-react';
export function HomeHero() {
const content = useContent('home-hero', {
fallback: {
title: 'Welcome to Croct!',
subtitle: 'The easiest way to personalize your application.',
button: {
label: 'Get started',
link: '/signup',
}
},
});
return (
<div>
<strong>{content.title}</strong>
<p>{content.subtitle}</p>
<a href={content.button.link}>{content.button.label}</a>
</div>
);
}

The SDK takes care of the rest, ensuring that your application will always have content to render, even if the fetch fails.

Version control

You can lock a specific slot version to keep the content structure aligned with your application's expectations. This gives your team the freedom to evolve the structure over time without the risk of breaking things.

You can specify the version of the slot by passing a versioned ID in the form <id>@<version>. For example, passing home-hero@2 will fetch the content for the home-hero slot in version 2. Not specifying a version number is the same as passing home-hero@latest, which will load the latest content.

components/HomeHero.jsx
12345678910111213
import {useContent} from '@croct/plug-react';
export function HomeHero() {
const content = useContent('home-hero@2');
return (
<div>
<strong>{content.title}</strong>
<p>{content.subtitle}</p>
<a href={content.button.link}>{content.button.label}</a>
</div>
);
}

For more information, see  Slot versioning.

Localization

To support multiple locales, you can use the preferredLocale option to specify the locale of the content you want to retrieve. This is usually the locale of the user's browser or account.

By default, if you do not specify a locale, or if the content is not available in the preferred locale, the content is returned in the default locale of your workspace .

Here is an example of how to fetch content in a specific locale:

components/HomeHero.jsx
123456789101112131415
import {useContent} from '@croct/plug-react';
export function HomeHero() {
const content = useContent('home-hero', {
preferredLocale: 'en-ca',
});
return (
<div>
<strong>{content.title}</strong>
<p>{content.subtitle}</p>
<a href={content.button.link}>{content.button.label}</a>
</div>
);
}

For more information, refer to the preferredLocale documentation.

Context variables

Sometimes you need to provide additional information to personalize or segment your users.

For example, if you are working on a SaaS application, you may want to personalize the content based on the subscription plan, quota usage, features, or any other application-specific information. You can achieve this by passing any relevant information to the attributes option:

components/HomeHero.jsx
123456789101112131415
import {useContent} from '@croct/plug-react';
export function UpgradeBanner() {
const content = useContent('upgrade-banner', {
attributes: {plan: 'premium'},
});
return (
<div>
<strong>{content.title}</strong>
<p>{content.subtitle}</p>
<a href={content.button.url}>{content.button.label}</a>
</div>
);
}

These values are then accessible as custom attributes in the context variable:

context's plan is "premium"

Keep in mind that the context has some constraints on the number of attributes and levels of nesting. For more information, please refer to the attributes documentation.