
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
@dotcms/react
Advanced tools
The @dotcms/react
SDK is the DotCMS official React library. It empowers React developers to build powerful, editable websites and applications in no time.
For Production Use:
For Testing & Development:
For Local Development:
For a step-by-step guide on setting up the Universal Visual Editor, check out our easy-to-follow instructions and get started in no time!
[!TIP] Make sure your API Token has read-only permissions for Pages, Folders, Assets, and Content. Using a key with minimal permissions follows security best practices.
This integration requires an API Key with read-only permissions for security best practices:
For detailed instructions, please refer to the dotCMS API Documentation - Read-only token.
npm install @dotcms/react@latest
This will automatically install the required dependencies:
@dotcms/uve
: Enables interaction with the Universal Visual Editor for real-time content editing@dotcms/client
: Provides the core client functionality for fetching and managing dotCMS dataimport { createDotCMSClient } from '@dotcms/client';
type DotCMSClient = ReturnType<typeof createDotCMSClient>;
export const dotCMSClient: DotCMSClient = createDotCMSClient({
dotcmsUrl: 'https://your-dotcms-instance.com',
authToken: 'your-auth-token', // Optional for public content
siteId: 'your-site-id' // Optional site identifier/name
});
Configure a proxy to leverage the powerful dotCMS image API, allowing you to resize and serve optimized images efficiently. This enhances application performance and improves user experience, making it a strategic enhancement for your project.
// vite.config.ts
import { defineConfig } from 'vite';
import dns from 'node:dns';
dns.setDefaultResultOrder('verbatim');
export default defineConfig({
server: {
proxy: {
'/dA': {
target: 'your-dotcms-instance.com',
changeOrigin: true
}
}
}
});
Learn more about Vite configuration here.
Once configured, image URLs in your components will automatically be proxied to your dotCMS instance:
π Learn more about Image Resizing and Processing in dotCMS with React.
// /components/my-dotcms-image.tsx
import type { DotCMSBasicContentlet } from '@dotcms/types';
export const MyDotCMSImageComponent = ({ inode, title }: DotCMSBasicContentlet) => {
return <img src={`/dA/${inode}`} alt={title} />;
}
The following example demonstrates how to quickly set up a basic dotCMS page renderer in your React application. This example shows how to:
// /src/app/pages/dotcms-page.tsx
import { useState, useEffect } from 'react';
import { DotCMSLayoutBody, useEditableDotCMSPage } from '@dotcms/react';
import { DotCMSPageResponse } from '@dotcms/types';
import { dotCMSClient } from './dotCMSClient';
import { BlogComponent } from './BlogComponent';
import { ProductComponent } from './ProductComponent';
const COMPONENTS_MAP = {
Blog: BlogComponent,
Product: ProductComponent
};
const MyPage = () => {
const [response, setResponse] = useState<DotCMSPageResponse | null>(null);
const { pageAsset } = useEditableDotCMSPage(response);
useEffect(() => {
dotCMSClient.page.get('/').then((response) => {
setResponse(response);
});
}, []);
return <DotCMSLayoutBody page={pageAsset} components={COMPONENTS_MAP} mode="development" />;
};
export default MyPage;
Looking to get started quickly? We've got you covered! Our Next.js starter project is the perfect launchpad for your dotCMS + Next.js journey. This production-ready template demonstrates everything you need:
π¦ Fetch and render dotCMS pages with best practices π§© Register and manage components for different content types π Listing pages with search functionality π Detail pages for blogs π Image and assets optimization for better performance β¨ Enable seamless editing via the Universal Visual Editor (UVE) β‘οΈ Leverage React's hooks and state management for optimal performance
[!TIP] This starter project is more than just an example, it follows all our best practices. We highly recommend using it as the base for your next dotCMS + Next.js project!
All components and hooks should be imported from @dotcms/react
:
DotCMSLayoutBody
is a component used to render the layout for a DotCMS page, supporting both production and development modes.
Input | Type | Required | Default | Description |
---|---|---|---|---|
page | DotCMSPageAsset | β | - | The page asset containing the layout to render |
components | DotCMSPageComponent | β | {} | Map of content type β React component |
mode | DotCMSPageRendererMode | β | 'production' | Rendering mode ('production' or 'development') |
β οΈ Important: This is a client-side React component.
DotCMSLayoutBody
uses React features likeuseContext
,useEffect
, anduseState
. If you're using a framework that supports Server-Side Rendering (like Next.js, Gatsby, or Astro), you must mark the parent component with"use client"
or follow your frameworkβs guidelines for using client-side components.
import type { DotCMSPageAsset } from '@dotcms/types';
import { DotCMSLayoutBody } from '@dotcms/react';
import { MyBlogCard } from './MyBlogCard';
import { DotCMSProductComponent } from './DotCMSProductComponent';
const COMPONENTS_MAP = {
Blog: MyBlogCard,
Product: DotCMSProductComponent
};
const MyPage = ({ pageAsset }: DotCMSPageResponse) => {
return <DotCMSLayoutBody page={pageAsset} components={COMPONENTS_MAP} />;
};
production
: Performance-optimized mode that only renders content with explicitly mapped components, leaving unmapped content empty.development
: Debug-friendly mode that renders default components for unmapped content types and provides visual indicators and console logs for empty containers and missing mappings.The DotCMSLayoutBody
component uses a components
prop to map content type variable names to React components. This allows you to render different components for different content types. Example:
const DYNAMIC_COMPONENTS = {
Blog: MyBlogCard,
Product: DotCMSProductComponent
};
Blog
, Product
): Match your content type variable names in dotCMS[!TIP] Always use the exact content type variable name from dotCMS as the key. You can find this in the Content Types section of your dotCMS admin panel.
DotCMSEditableText
is a component for inline editing of text fields in dotCMS, supporting plain text, text area, and WYSIWYG fields.
Input | Type | Required | Description |
---|---|---|---|
contentlet | T extends DotCMSBasicContentlet | β | The contentlet containing the editable field |
fieldName | keyof T | β | Name of the field to edit, which must be a valid key of the contentlet type T |
mode | 'plain' | 'full' | β | plain (default): Support text editing. Does not show style controls. full : Enables a bubble menu with style options. This mode only works with WYSIWYG fields. |
format | 'text' | 'html' | β | text (default): Renders HTML tags as plain text html : Interprets and renders HTML markup |
import type { DotCMSBasicContentlet } from '@dotcms/types';
import { DotCMSEditableText } from '@dotcms/react';
const MyBannerComponent = ({ contentlet }: { contentlet: DotCMSBasicContentlet }) => {
const { inode, title, link } = contentlet;
return (
<div className="flex overflow-hidden relative justify-center items-center w-full h-96 bg-gray-200">
<img className="object-cover w-full" src={`/dA/${inode}`} alt={title} />
<div className="flex absolute inset-0 flex-col justify-center items-center p-4 text-center text-white">
<h2 className="mb-2 text-6xl font-bold text-shadow">
<DotCMSEditableText fieldName="title" contentlet={contentlet} />
</h2>
<a
href={link}
className="p-4 text-xl bg-red-400 rounded-sm transition duration-300 hover:bg-red-500">
See more
</a>
</div>
</div>
);
};
export default MyBannerComponent;
Save
workflow action on blur without needing full content dialog.DotCMSBlockEditorRenderer
is a component for rendering Block Editor content from dotCMS with support for custom block renderers.
Input | Type | Required | Description |
---|---|---|---|
blocks | BlockEditorContent | β | The Block Editor content to render |
customRenderers | CustomRenderers | β | Custom rendering functions for specific block types |
className | string | β | CSS class to apply to the container |
style | CSSProperties | β | Inline styles for the container |
import type { DotCMSBasicContentlet } from '@dotcms/types';
import { DotCMSBlockEditorRenderer } from '@dotcms/react';
import { MyCustomBannerBlock } from './MyCustomBannerBlock';
import { MyCustomH1 } from './MyCustomH1';
const CUSTOM_RENDERERS = {
customBannerBlock: MyCustomBannerBlock,
h1: MyCustomH1
};
const DetailPage = ({ contentlet }: { contentlet: DotCMSBasicContentlet }) => {
return (
<DotCMSBlockEditorRenderer
blocks={contentlet['YOUR_BLOCK_EDITOR_FIELD']}
customRenderers={CUSTOM_RENDERERS}
/>
);
};
DotCMSEditableText
DotCMSBlockEditorRenderer
only works with Block Editor fields. For other fields, use DotCMSEditableText
.π For advanced examples, customization options, and best practices, refer to the DotCMSBlockEditorRenderer README.
DotCMSShow
is a component for conditionally rendering content based on the current UVE mode. Useful for mode-based behaviors outside of render logic.
Input | Type | Required | Description |
---|---|---|---|
children | ReactNode | β | Content to be conditionally rendered |
when | UVE_MODE | β | The UVE mode when content should be displayed: UVE_MODE.EDIT : Only visible in edit mode UVE_MODE.PREVIEW : Only visible in preview mode UVE_MODE.PUBLISHED : Only visible in published mode |
import { UVE_MODE } from '@dotcms/types';
import { DotCMSShow } from '@dotcms/react';
const MyComponent = () => {
return (
<DotCMSShow when={UVE_MODE.EDIT}>
<div>This will only render in UVE EDIT mode</div>
</DotCMSShow>
);
};
π Learn more about the UVE_MODE
enum in the dotCMS UVE Package Documentation.
useEditableDotCMSPage
is a hook that enables real-time page updates when using the Universal Visual Editor.
Param | Type | Required | Description |
---|---|---|---|
pageResponse | DotCMSPageResponse | β | The page data object from client.page.get() |
When you use the hook, it:
'use client';
import { useEditableDotCMSPage, DotCMSLayoutBody } from '@dotcms/react';
import type { DotCMSPageResponse } from '@dotcms/types';
const COMPONENTS_MAP = {
Blog: BlogComponent,
Product: ProductComponent
};
export function DotCMSPage({ pageResponse }: { pageResponse: DotCMSPageResponse }) {
const { pageAsset } = useEditableDotCMSPage(pageResponse);
return <DotCMSLayoutBody pageAsset={pageAsset} components={COMPONENTS_MAP} />;
}
useDotCMSShowWhen
is a hook for conditionally showing content based on the current UVE mode. Useful for mode-based behaviors outside of render logic.
Param | Type | Required | Description |
---|---|---|---|
when | UVE_MODE | β | The UVE mode when content should be displayed: UVE_MODE.EDIT : Only visible in edit mode UVE_MODE.PREVIEW : Only visible in preview mode UVE_MODE.PUBLISHED : Only visible in published mode |
import { UVE_MODE } from '@dotcms/types';
import { useDotCMSShowWhen } from '@dotcms/react';
const MyEditButton = () => {
const isEditMode = useDotCMSShowWhen(UVE_MODE.EDIT); // returns a boolean
if (isEditMode) {
return <button>Edit</button>;
}
return null;
};
DotCMSEditablePageService
call to enable UVE.dotcmsUrl
matches your instance URL exactlyComponents Not Rendering: Empty spaces where content should appear
components
propdevelopment
mode for detailed loggingAsset Loading Issues: Images or files not loading
vite.config.ts
/dA
path is properly configuredBuild Errors: npm install
fails
npm cache clean --force
node_modules
and reinstallRuntime Errors: Console errors about missing imports or components not rendering
@dotcms/react
Server Component Errors: Errors about React class components in Server Components
Possible Causes:
Solutions:
Split your code into Server and Client Components:
// app/page.tsx (Server Component)
import { DotCMSPage } from '@/components/DotCMSPage';
import { dotCMSClient } from '@/lib/dotCMSClient';
export default async function Page() {
const pageResponse = await dotCMSClient.page.get('/index');
return <DotCMSPage pageResponse={pageResponse} />;
}
// components/DotCMSPage.tsx (Client Component)
'use client';
import { useEditableDotCMSPage, DotCMSLayoutBody } from '@dotcms/react';
import type { DotCMSPageResponse } from '@dotcms/types';
const COMPONENTS_MAP = {
Blog: BlogComponent,
Product: ProductComponent
};
export function DotCMSPage({ pageResponse }: { pageResponse: DotCMSPageResponse }) {
const { pageAsset } = useEditableDotCMSPage(pageResponse);
return <DotCMSLayoutBody pageAsset={pageAsset} components={COMPONENTS_MAP} />;
}
Always fetch data in Server Components for better performance
Use Client Components only for rendering dotCMS interactive components
Enable Development Mode
<dotcms-layout-body
[page]="pageAsset()"
[components]="components()"
mode="development"
/>
This will:
Check Browser Console
Network Monitoring
If you're still experiencing problems after trying these solutions:
We offer multiple channels to get help with the dotCMS React SDK:
dotcms-react
when posting questions.When reporting issues, please include:
GitHub pull requests are the preferred method to contribute code to dotCMS. We welcome contributions to the DotCMS UVE SDK! If you'd like to contribute, please follow these steps:
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)Please ensure your code follows the existing style and includes appropriate tests.
dotCMS comes in multiple editions and as such is dual-licensed. The dotCMS Community Edition is licensed under the GPL 3.0 and is freely available for download, customization, and deployment for use within organizations of all stripes. dotCMS Enterprise Editions (EE) adds several enterprise features and is available via a supported, indemnified commercial license from dotCMS. For the differences between the editions, see the feature page.
This SDK is part of dotCMS's dual-licensed platform (GPL 3.0 for Community, commercial license for Enterprise).
Learn more at dotcms.com.
FAQs
Official React Components library to render a dotCMS page.
The npm package @dotcms/react receives a total of 848 weekly downloads. As such, @dotcms/react popularity was classified as not popular.
We found that @dotcms/react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600Γ faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.