Skip to content
A set of components and utilities to work faster with DatoCMS in React environments
Branch: master
Clone or download

Latest commit

Latest commit 350ed75 Mar 25, 2020


Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Use <svg /> as sizer, as it allows more flexibility in sizing Mar 20, 2020
src Hide placeholder when image is loaded Mar 24, 2020
.gitignore Add Next.js example Dec 10, 2019
.travis.yml v1.0.2 Dec 3, 2019 Changelog Mar 24, 2020
LICENSE Create LICENSE Dec 11, 2019 Fix typo Mar 25, 2020
package-lock.json 1.2.1 Mar 24, 2020
package.json 1.2.1 Mar 24, 2020
tsconfig.json v1.0.2 Dec 3, 2019


MIT MIT Build Status

A set of components and utilities to work faster with DatoCMS in React environments. Integrates seamlessy with DatoCMS's GraphQL Content Delivery API.

  • TypeScript ready;
  • CSS-in-JS ready;
  • Compatible with any GraphQL library (Apollo, graphql-hooks, graphql-request, etc.);
  • Usable both client and server side;
  • Compatible with vanilla React, Next.js and pretty much any other React-based solution;

Table of Contents



npm install react-datocms

Progressive/responsive image

<Image /> is a React component specially designed to work seamlessly with DatoCMS’s responsiveImage GraphQL query that optimizes image loading for your sites.

Out-of-the-box features

  • Offer WebP version of images for browsers that support the format
  • Generate multiple smaller images so smartphones and tablets don’t download desktop-sized images
  • Efficiently lazy load images to speed initial page load and save bandwidth
  • Use either blur-up or background color techniques to show a preview of the image while it loads
  • Hold the image position so your page doesn’t jump while images load


  1. Import Image from react-datocms and use it in place of the regular <img /> tag
  2. Write a GraphQL query to your DatoCMS project using the responsiveImage query

The GraphQL query returns multiple thumbnails with optimized compression. The Image component automatically sets up the “blur-up” effect as well as lazy loading of images further down the screen.


For a fully working example take a look at our examples directory.

import React from "react";
import { Image } from "react-datocms";

const Page = ({ data }) => (
    <Image data={data.blogPost.cover.responsiveImage} />

const query = gql`
  query {
    blogPost {
      cover {
          imgixParams: { fit: crop, w: 300, h: 300, auto: format }
        ) {
          # HTML5 src/srcset/sizes attributes

          # size information (post-transformations)

          # SEO attributes

          # background color placeholder or...

          # blur-up placeholder, JPEG format, base64-encoded

export default withQuery(query)(Page);


prop type default required description
data ResponsiveImage object The actual response you get from a DatoCMS responsiveImage GraphQL query.
className string null Additional CSS class of root node
style CSS properties null Additional CSS rules to add to the root node
pictureClassName string null Additional CSS class for the inner <picture /> tag
pictureStyle CSS properties null Additional CSS rules to add to the inner <picture /> tag
fadeInDuration integer 500 Duration (in ms) of the fade-in transition effect upoad image loading
intersectionTreshold float 0 Indicate at what percentage of the placeholder visibility the loading of the image should be triggered. A value of 0 means that as soon as even one pixel is visible, the callback will be run. A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
intersectionMargin string "0px 0px 0px 0px" Margin around the placeholder. Can have values similar to the CSS margin property (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the placeholder element's bounding box before computing intersections.
lazyLoad Boolean true Wheter enable lazy loading or not
explicitWidth Boolean false Wheter the image wrapper should explicitely declare the width of the image or keep it fluid

The ResponsiveImage object

The data prop expects an object with the same shape as the one returned by responsiveImage GraphQL call. It's up to you to make a GraphQL query that will return the properties you need for a specific use of the <Image> component.

  • The minimum required properties for data are: aspectRatio, width, sizes, srcSet and src;
  • alt and title, while not mandatory, are all highly suggested, so remember to use them!
  • You either want to add the webpSrcSet field or specify { auto: format } in your imgixParams, to automatically use WebP images in browsers that support the format;
  • If you provide both the bgColor and base64 property, the latter will take precedence, so just avoiding querying both fields at the same time, it will only make the response bigger 😉

Here's a complete recap of what responsiveImage offers:

property type required description
aspectRatio float The aspect ratio (width/height) of the image
width integer The width of the image
sizes string The HTML5 sizes attribute for the image
srcSet string The HTML5 srcSet attribute for the image
src string The fallback src attribute for the image
webpSrcSet string The HTML5 srcSet attribute for the image in WebP format, for browsers that support the format
alt string Alternate text (alt) for the image
title string Title attribute (title) for the image
bgColor string The background color for the image placeholder
base64 string A base64-encoded thumbnail to offer during image loading

Social share, SEO and Favicon meta tags

Just like the image component, renderMetaTags() is a helper specially designed to work seamlessly with DatoCMS’s _seoMetaTags and faviconMetaTags GraphQL queries so that you can handle proper SEO in your pages with a simple one-liner.


renderMetaTags() takes an array of Tags in the exact form they're returned by the following DatoCMS GraphQL API queries:

  • _seoMetaTags query on any record, or
  • faviconMetaTags on the global _site object.

You can concat multiple array of Tags together and pass them to a single renderMetaTags() call.


For a working example take a look at our examples directory.

import React from "react";
import { renderMetaTags } from "react-datocms";

const Page = ({ data }) => (

const query = gql`
  query {
    page: homepage {
      seo: _seoMetaTags {

    site: _site {
      favicon: faviconMetaTags {

export default withQuery(query)(Page);
You can’t perform that action at this time.