React migration guide

Introduction

Cloudinary's React SDK v2 (Beta) is designed to provide a simpler and more enhanced developer experience. This guide explains how to migrate your React code from the cloudinary-react library (SDK v1) to the React SDK v2, which includes @cloudinary/react from the frontend-frameworks repo together with @cloudinary/url-gen from the js-url-gen repo.

Key improvements in the React SDK v2:

  • A new action-based syntax, designed to make building delivery URLs and transformations more logical and discoverable.
    • When compiled to the final delivery URL, each action (including any action qualifiers) represents a different component (separated by a '/'), for example: /c_scale,w_400/f_auto/q_auto/, rather than /c_scale,f_auto,q_auto,w_400/.
  • Newly added autocomplete to list options and parameters and ensure only those that are supported can be used together.
  • A smaller bundle size - you only need to import what you want to use.

Things to know before migrating to the React SDK v2

The action-based syntax used in the React SDK v2 may cause URLs to be formed differently from those generated by the React SDK v1.

For example:

  • URL generated with SDK v1:
    https://res.cloudinary.com/demo/image/upload/c_scale,f_auto,q_auto,w_400/sample.jpg
  • URL generated with SDK v2:
    https://res.cloudinary.com/demo/image/upload/c_scale,w_400/f_auto/q_auto/sample.jpg


Even if the delivered media file looks and behaves identically, changes to URLs can have the following implications:

  • You may see a one-time increase in transformation counts
  • You may see a one-time increase in storage usage for the new derived assets
  • You may see a one-time increase in add-on usage when add-on transformation parameters are used in the URL
  • CDN caches may need to be warmed with the new derived assets
  • If strict transformations are enabled on your account, you need to allow the new transformations
  • If you require transformations to be generated eagerly, for example long videos, you need to regenerate these via the latest SDK, using the eager parameter of the explicit method.

To reduce the impact of all of the above, we recommend using the createCloudinaryLegacyURL method for your existing transformation URLs, especially if your existing app delivers a large number of transformed assets. This maintains the formations of the transformations, so the URLs remain the same.

The createCloudinaryLegacyURL function supports only transformation and configuration parameters. It does not help to migrate HTML tags, responsive, placeholder, transparent video or jQuery functionality.

For all new transformation URLs that you add to your application, we recommend using the new action-based SDK syntax offered by the latest version of the SDK.

For full documentation on the React SDK v2, see the React SDK v2 guide.

Key considerations

Cloudinary's React library (@cloudinary/react) must be used in conjunction with the Cloudinary JavaScript SDK v2 library (@cloudinary/url-gen) to provide all of Cloudinary's transformation and optimization functionality. As such, the React SDK v2 is very different from the React SDK v1 in its architecture and usage, so migration paths depend on your current usage of the cloudinary-react library.

You can use the cloudinary-react, @cloudinary/react and @cloudinary/url-gen packages in your application concurrently, so although not recommended for the long term due to the increased bundle size, you could start by integrating @cloudinary/react and @cloudinary/url-gen into your application and slowly migrate your functionality piece by piece, until you are able to remove all cloudinary-react functionality.

Installation

Install the required packages using the NPM package manager:

Copy to clipboard
npm i @cloudinary/url-gen @cloudinary/react --save

Migrating Cloudinary instance configuration

Using the React SDK v1, you may be configuring your cloud name and other configuration parameters in a CloudinaryContext. This is similar to configuring these parameters in a Cloudinary instance, provided by the @cloudinary/url-gen library, in that the configuration is set once, rather than for each image or video.

For example, setting cloudName and secure in a CloudinaryContext element:

Copy to clipboard
// SDK v1
import {CloudinaryContext} from "cloudinary-react";

<CloudinaryContext cloudName="demo" secure="true">
  <div>
    ...
  </div>
</CloudinaryContext>

is similar to setting cloudName and secure in a Cloudinary instance:

Copy to clipboard
// SDK v2
import {Cloudinary} from "@cloudinary/url-gen";

var cl = new Cloudinary({cloud: {cloudName: "demo"}, url: {secure: true}});

Migrating asset instance configuration

You can also set configuration parameters in Image or Video elements in the React SDK v1. This is similar to setting the parameters on a per asset instance.

For example, setting cloudName in an Image element:

Copy to clipboard
// SDK v1
import {Image} from 'cloudinary-react';

<Image cloudName="demo" publicId="sample"/>

is similar to setting cloudName in a CloudinaryImage:

Copy to clipboard
// SDK v2
import {CloudinaryImage} from "@cloudinary/url-gen";

const myImage = new CloudinaryImage("sample", {cloudName: "demo"});

Note
If you use the Cloudinary instance configuration, you don't need to add configuration parameters to your asset instances:

Copy to clipboard
const myImage = cl.image("sample");

Migrating delivery URLs

The Image component in cloudinary-react has been replaced by the AdvancedImage component in @cloudinary/react.

Whereas in the React SDK v1 you specify transformation parameters in the Image component, in the SDK v2 they are specified in @cloudinary/url-gen in a CloudinaryImage object that you then pass to the AdvancedImage component.

Here is an example of an Image component with configuration and transformation parameters included:

Copy to clipboard
// SDK v1
<Image publicId="actor.jpg" cloudName="demo" >
  <Transformation effect="cartoonify" />
  <Transformation radius="max" />
  <Transformation effect="outline:100" color="lightblue" />
  <Transformation background="lightblue" />
  <Transformation height="300" crop="scale" />
</Image>

There is no easy migration path from an Image component to an AdvancedImage component, but you can use the following process as an intermediate step, to create a URL that you can use as the source for a regular image tag:

  1. Convert the Image component to JSON. An XML to JSON converter can help with this transition, or you may want to use a script if you have a large number of URLs to convert. The previous example would become:

    Copy to clipboard
    {
        cloud_name: "demo",
        transformation: [
            {effect: "cartoonify"},
            {radius: "max"},
            {effect: "outline:100", color: "lightblue"},
            {background: "lightblue"},
            {height: 300, crop: "scale"}
        ]
    }
  2. Pass this JSON to the createCloudinaryLegacyURL function, included in the @cloudinary/url-gen library, to return a delivery URL that includes the transformations. Configuration parameters, such as cloud_name, should be included in the function call as this is simply a helper function to build the delivery URL:

    Copy to clipboard
    // SDK v2
    import {createCloudinaryLegacyURL} from "@cloudinary/url-gen";
    
    const migrateUrl = createCloudinaryLegacyURL("actor", {
        cloud_name: "demo",
        transformation: [
        {effect: "cartoonify"},
        {radius: "max"},
        {effect: "outline:100", color: "lightblue"},
        {background: "lightblue"},
        {height: 300, crop: "scale"}
        ]
    });
  3. Use this URL as the source for a regular image tag.

    Copy to clipboard
    // SDK v2
    const imgElement = document.createElement("img");
    imgElement.src = migrateUrl;
    // Append the image element to the DOM, for example:
    document.getElementById('div1').appendChild(imgElement);
    Copy to clipboard
    <!--Where div1 is the ID of a div element in your HTML file-->
    <div id="div1"></div>

Caution
If you have a large number of assets, we recommend you migrate using the createCloudinaryLegacyURL method. If you replace your existing transformations using the new SDK transformation syntax, you may find your URLs are generated in a slightly different way. See Things to know before migrating to the React SDK v2, for the implications of these changes to transformation URLs.

For all new Cloudinary delivery URLs, you should start to use the @cloudinary/url-gen syntax to create a CloudinaryImage that you can use in the AdvancedImage React component:

Copy to clipboard
// SDK v2
import React from "react"
import {AdvancedImage} from "@cloudinary/react";
import {CloudinaryImage} from "@cloudinary/url-gen";
import {scale} from "@cloudinary/url-gen/actions/resize";
import {outline, cartoonify} from "@cloudinary/url-gen/actions/effect";
import {max} from "@cloudinary/url-gen/actions/roundCorners";
import {outer} from "@cloudinary/url-gen/qualifiers/outlineMode";

const App = () => {

    const myImage = new CloudinaryImage("actor", {cloudName: "demo"});

    // Apply the transformation.
    myImage
    .effect(cartoonify())
    .roundCorners(max())
    .effect(outline().mode(outer()).width(100).color("lightblue"))
    .backgroundColor("lightblue")
    .resize(scale().height(300));


    // Render the transformed image in a React component.
    return (
        <div>
        <AdvancedImage cldImg={myImage} />
        </div>
    )
};

The resulting URL is:

Transformed actor

Migrating advanced image components and responsive functionality

The advanced image components (lazy loading, image placeholders and image accessibility) and responsive image settings offered by cloudinary-react, are offered as plugins in the React SDK v2.

When you have migrated your delivery URLs to use the AdvancedImage component, you can use the plugins as follows:

Note
This example shows all four plugins being used, but you can use only one, or any combination, by importing and specifying only those you need.

Copy to clipboard
// SDK v2
import React from "react"
import {CloudinaryImage} from "@cloudinary/url-gen";

// Import the plugins.
import {AdvancedImage, lazyload, accessibility, responsive, placeholder} from "@cloudinary/react";

 const App = () => {

  const myImage = new CloudinaryImage("sample", {cloudName: "demo"});

  // Use all the plugins.
  return (
    <div>
      <AdvancedImage cldImg={myImage} plugins={[lazyload(), responsive(), accessibility(), placeholder()]}/>
    </div>
  )
};

Related topics

✔️ Feedback sent!

Rate this page: