Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@contensis/canvas-react
Advanced tools
Render content curated in a Contensis canvas field in your React projects.
Install with your project's preferred package manager
npm install --save @contensis/canvas-react
yarn add --save @contensis/canvas-react
Render canvas content with React
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { RenderContextProvider, Renderer } from '@contensis/canvas-react';
import * as CanvasData from './canvas-data';
// Our React App
const App = () => {
const [data] = useState(CanvasData.data); // Demo data
return (
<div className="content">
<RenderContextProvider>
<Renderer data={data} />
</RenderContextProvider>
</div>
);
};
const element = document.getElementById('root') as HTMLElement;
ReactDOM.createRoot(element).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
You can override the default rendering for content blocks by adding your own render components when creating the canvas renderer
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Block, Image, ImageBlock, RenderBlockProps, RenderContextProvider, Renderer, Table, TableBlock } from '@contensis/canvas-react';
import * as CanvasData from './canvas-data';
const MyImage = (props: RenderBlockProps<ImageBlock>) => {
// Embelish the image markup if a caption is included
const caption = props.block?.value?.caption;
// Set a className for the image in all cases
return !!caption ? (
<figure className="figure" style={{ display: 'block' }}>
<Image {...props} className="figure-img img-fluid" />
<figcaption className="figure-caption text-end">{caption}</figcaption>
</figure>
) : (
<Image {...props} className="img-fluid" />
);
};
const MyTable = (props: RenderBlockProps<TableBlock>) => {
// Set CSS className on tables
return <Table {...props} className="table table-striped" />;
};
// Component wrapping a Renderer for simple usage
const SimpleRenderer = ({ data }: { data: Block[] }) => {
return (
<RenderContextProvider
blocks={{
_image: MyImage,
_table: MyTable
}}
>
<Renderer data={data} />
</RenderContextProvider>
);
};
// Our React App
const App = () => {
const [data] = useState(CanvasData.data); // Demo data
return (
<div className="content">
<SimpleRenderer data={data} />
</div>
);
};
const element = document.getElementById('root') as HTMLElement;
ReactDOM.createRoot(element).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
We can do the same for any Component fields present in the Canvas data
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Block, Image, RenderContextProvider, Renderer, Table } from '@contensis/canvas-react';
import * as CanvasData from './canvas-data';
type Book = { cover: string; name: string };
// Render a "book" component within the canvas data
const MyBookComponent = (props: RenderBlockProps<ComponentBlock<Book>) => {
const book = props.block?.value;
if (!book) return null;
return (
<div className="card mb-3">
<div className="row g-0">
<div className="col-md-8">
<div className="card-body">
<h5 className="card-title">{book.name}</h5>
<p className="card-text">{book.name}</p>
</div>
</div>
<div className="col-md-4">
<img src={book.cover} className="img-fluid rounded-start" />
</div>
</div>
</div>
);
}
// Component wrapping a customised Renderer for use in our app
const SimpleRenderer = ({ data }: { data: Block[] }) => {
return (
<RenderContextProvider
components={{
book: MyBookComponent,
}}
>
<Renderer data={data} />
</RenderContextProvider>
);
};
// Our React App
const App = () => {
const [data] = useState(CanvasData.data); // Demo data
return (
<div className="content">
<SimpleRenderer data={data} />
</div>
);
};
const element = document.getElementById('root') as HTMLElement;
ReactDOM.createRoot(element).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
You try this out with the React example project
FAQs
Render canvas content with React
The npm package @contensis/canvas-react receives a total of 364 weekly downloads. As such, @contensis/canvas-react popularity was classified as not popular.
We found that @contensis/canvas-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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.