• API References

@prismicio/react - v3

Overview

@prismicio/react is the official Prismic package for creating React-based websites.

With this package, you can:

This page documents all APIs provided by @prismicio/react.

Install

@prismicio/react is installed when bootstrapping a Next.js project with @slicemachine/init.

npx @slicemachine/init@latest

It can also be manually installed in any React project:

npm install --save @prismicio/react @prismicio/client

@prismicio/client is a peer dependency and must be installed.

API

All components can be imported as a named import.

The following example imports PrismicImage:

import { PrismicImage } from "@prismicio/react";

<PrismicImage>

Displays an optimized image from an image field.

<PrismicImage field={slice.primary.image} />
prop
Description
Default
field
ImageField

An image field to display.

imgixParams
optional
object

An object of imgix URL parameters. See an example.

alt
optional
""
Declare an image as decorative.
fallbackAlt
optional
""

Declare an image as decorative if the image field does not have alt text.

widths
optional
number[] | "thumbnails" | "defaults"

Widths used to build a srcset value for the image field.

  • "thumbnails" uses the image field’s thumbnails.
  • "defaults" uses [640, 750, 828, 1080, 1200, 1920, 2048, 3840].

Only one of widths or pixelDensities can be used.

"defaults"

pixelDensities
optional
number[] | "defaults"

Pixel densities used to build a srcset value for the image field.

  • "defaults" uses [1, 2, 3].

Only one of widths or pixelDensities can be used.

fallback
optional
ReactNode

Rendered if the image is empty.

Displays a link from a link field or Prismic document.

<PrismicLink field={slice.primary.link} />
prop
Description
Default
field
LinkField

A link field to link. Only one of field, document, or href can be used.

document
PrismicDocument

A Prismic document to link. Only one of field, document, or href can be used.

href
string

A URL to link. Only one of field, document, or href can be used.

rel
optional
undefined | string | ((metadata) => string | undefined)

A ref attribute. A function can be passed to determine the value based on the link. Use undefined to remove the rel attribute.

"noreferrer" if the link is external.

linkResolver
optional
(field: ContentRelationshipField) => string | null | undefined

A link resolver function used to determine a URL from the field or document prop. If it returns a value, it takes priority over a route resolver.

internalComponent
optional
ComponentType

The component used to render internal links.

<a>

externalComponent
optional
ComponentType

The component used to render external links.

<a>

<PrismicText>

Displays plain text from a rich text field.

<PrismicText field={slice.primary.text} />
prop
Description
Default
field
RichTextField

A rich text field to render.

fallback
optional
string

Rendered if the rich text field is empty.

separator
optional
string

Separator used between text blocks.

" "

<PrismicRichText>

Displays formatted text from a rich text field.

<PrismicRichText field={slice.primary.text} />
prop
Description
Default
field
RichTextField

A rich text field to render.

fallback
optional
ReactNode

Rendered if the rich text field is empty.

components
optional
object

A map of rich text block types to React components. See an example.

Standard HTML elements (e.g. heading1 becomes a <h1>).

linkResolver
optional
(field: ContentRelationshipField) => string | null | undefined

A link resolver function used to determine a URL from the field or document prop. If it returns a value, it takes priority over a route resolver.

internalLinkComponent
optional
ComponentType

The component used to render internal links.

<a>

internalLinkComponent
optional
ComponentType

The component used to render external links.

<a>

<PrismicTable>

Displays a formatted table from a table field.

<PrismicTable field={slice.primary.my_table_field} />
prop
Description
Default
field
TableField

A table field to render.

fallback
optional
ReactNode

Rendered if the table field is empty.

components
optional
object

A map of table elements and rich text block types to React components. The available table elements are table, thead, tbody, tr, th, and td. The available rich text block types are paragraph, em, strong, and hyperlink. See an example.

Standard HTML elements (e.g. table becomes a <table>).

<SliceZone>

Renders slices from a slice zone.

<SliceZone
  slices={doc.data.slices}
  components={{
    text: ({ slice }) => (
      <section>
        <PrismicRichText field={slice.primary.text} />
      </section>
    ),
  }}
/>
prop
Description
Default
slices
SliceZone
A slice zone to render.
components
Record<string, ComponentType<SliceComponentProps>>

A map of slice types to React components.

defaultComponent
optional
ComponentType

Rendered if a slice does not have a component mapping.

context
optional
any

Arbitrary data made available to all slice components.

<PrismicToolbar>

Adds the Prismic toolbar.

<PrismicToolbar repositoryName="example-prismic-repo" />
prop
Description
Default
repositoryName
string

The name of the Prismic repository.

Upgrade from v2

Follow these steps to upgrade an existing project to @prismicio/react v3.