{"id":28425,"date":"2021-11-17T22:31:57","date_gmt":"2021-11-17T22:31:57","guid":{"rendered":"http:\/\/Build-an-E-commerce-Product-Image-Zoom-Lens-in-Next.js"},"modified":"2025-02-09T14:22:21","modified_gmt":"2025-02-09T22:22:21","slug":"build-an-e-commerce-product-image-zoom-lens-in-next-js","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/","title":{"rendered":"Build a Product Image Zoom Lens in Next.js"},"content":{"rendered":"<div class=\"wp-block-cloudinary-markdown \"><p>Customers of modern E-commerce systems, expect custom-made products and the option to see how their products will look before purchasing them.<\/p>\n<p>In this tutorial, we will use Next.js to create a product image zoom lens that allows us to zoom in on a product image while maintaining the anticipated image look and feel of the image after purchase.<\/p>\n<h2>CodeSandbox<\/h2>\n<p>This project was completed in a <a href=\"https:\/\/codesandbox.io\/s\/e-commerce-product-image-zoom-lens-in-next-js-bl4xz\">Codesandbox<\/a>. To get started quickly, \u201cfork\u201d the <a href=\"https:\/\/codesandbox.io\/s\/e-commerce-product-image-zoom-lens-in-next-js-bl4xz\">Codesandbox<\/a> or run the project.<\/p>\n<\/div>\n  \n  <div class=\"wp-block-cloudinary-code-sandbox \">\n    <iframe\n      src=\"https:\/\/codesandbox.io\/embed\/e-commerce-product-image-zoom-lens-in-next-js-bl4xz?theme=dark&amp;codemirror=1&amp;highlights=&amp;editorsize=50&amp;fontsize=14&amp;expanddevtools=0&amp;hidedevtools=0&amp;eslint=0&amp;forcerefresh=0&amp;hidenavigation=0&amp;initialpath=%2F&amp;module=&amp;moduleview=0&amp;previewwindow=&amp;view=&amp;runonclick=1\"\n      height=\"500\"\n      style=\"width: 100%;\"\n      title=\"E-commerce Product Image Zoom Lens in Next.js\"\n      loading=\"lazy\"\n      allow=\"accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking\"\n      sandbox=\"allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts\"\n    ><\/iframe>\n  <\/div>\n\n  <div class=\"wp-block-cloudinary-markdown \"><p>GitHub Repository: <a href=\"https:\/\/github.com\/Olanetsoft\/E-commerce-Product-Image-Zoom-Lens-in-Next.js\">https:\/\/github.com\/Olanetsoft\/E-commerce-Product-Image-Zoom-Lens-in-Next.js<\/a><\/p>\n<p>&lt;!\u2014 Can you please swap the link text, it is different than the actual git link \u2014&gt;<\/p>\n<h2>Getting Started with Next.js<\/h2>\n<p><a href=\"https:\/\/nextjs.org\/\">Next.js<\/a> is an open-source React-based front-end development web framework, that allows server-side rendering and the generation of static websites and applications.<\/p>\n<p>To create a new project, we use the <code>npx create-next-app<\/code> command to scaffold a new project in a directory of our choice.\nOnce the app is created and the dependencies automatically installed, we will see a message with instructions for navigating to our site and running it locally. We do this with the command<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">    cd <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">project<\/span> <span class=\"hljs-attr\">name<\/span>&gt;<\/span> &amp;&amp; npm start\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Next.js will start a hot-reloading development environment accessible by default at <a href=\"http:\/\/localhost:3000\">http:\/\/localhost:3000<\/a><\/p>\n<p>We will use <a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">react-image-magnify<\/a> to handle the product image zoom.<\/p>\n<p><a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">React-Image-Magnify<\/a> is a responsive React image zoom component for touch and mouse. It provides features like in-place and side-by-side image enlargement, positive or negative space guide lens options, interaction hint, configurable enlarged image dimensions, optional enlarged image external render, hover intent, etc.<\/p>\n<p>On the main page, we need the user interface to handle the product image zoom. We create this by updating the <code>pages\/index.js<\/code> file to a component:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">IndexPage<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    \n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"body\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"image\"<\/span>&gt;<\/span>\n            \/\/ Image here\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>E-commerce Product Image Zoom Lens in Next.js<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span>Touch<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>Hover image to magnify<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>As shown in the code snippet above, we created a simple layout to fit our image and a text description.<\/p>\n<p>In the project\u2019s root directory, we create a folder titled css, and in it, a stylesheet called <code>styles.css<\/code> with the following content.<\/p>\n<p>&lt;!\u2014 Based on your procedure, I would be unsure where to place this piece of code \u2014&gt;<\/p>\n<p>&lt;!\u2014 I think you might have skipped creating a css folder in root and a style.css file \u2014&gt;<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\">    <span class=\"hljs-selector-class\">.body<\/span> {\n      <span class=\"hljs-comment\">\/* max-width: 1024px; *\/<\/span>\n      <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n      <span class=\"hljs-attribute\">display<\/span>: flex;\n      <span class=\"hljs-attribute\">flex-direction<\/span>: column;\n    }\n    <span class=\"hljs-selector-class\">.image<\/span> {\n      <span class=\"hljs-attribute\">flex<\/span>: <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">100%<\/span>;\n    }\n    <span class=\"hljs-selector-class\">.text<\/span> {\n      <span class=\"hljs-attribute\">border-left<\/span>: <span class=\"hljs-number\">2px<\/span> solid grey;\n      <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">40px<\/span>;\n      <span class=\"hljs-attribute\">font-family<\/span>: sans-serif;\n    }\n    <span class=\"hljs-keyword\">@media<\/span> (<span class=\"hljs-attribute\">min-width:<\/span> <span class=\"hljs-number\">415px<\/span>) {\n      <span class=\"hljs-selector-class\">.body<\/span> {\n        <span class=\"hljs-attribute\">flex-direction<\/span>: row;\n      }\n      <span class=\"hljs-selector-class\">.image<\/span> {\n        <span class=\"hljs-attribute\">flex<\/span>: <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">50%<\/span>;\n      }\n    }\n    <span class=\"hljs-keyword\">@media<\/span> (<span class=\"hljs-attribute\">min-width:<\/span> <span class=\"hljs-number\">800px<\/span>) {\n      <span class=\"hljs-selector-class\">.image<\/span> {\n        <span class=\"hljs-attribute\">flex<\/span>: <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">33.5%<\/span>;\n      }\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p><a href=\"https:\/\/gist.github.com\/Olanetsoft\/ff5a07491d6a62ed372c2e333edc72b8.js\">https:\/\/gist.github.com\/Olanetsoft\/ff5a07491d6a62ed372c2e333edc72b8.js<\/a><\/p>\n<p>We also need to create an <code>_app.js<\/code> file inside the <code>pages<\/code> directory. This file is native to Next.js and wraps the whole application. We\u2019ll import the CSS file we created into this <code>_app.js<\/code> file.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"..\/css\/style.css\"<\/span>;\n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">MyApp<\/span>(<span class=\"hljs-params\">{ Component, pageProps }<\/span>) <\/span>{\n      <span class=\"hljs-keyword\">return<\/span> <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Component<\/span> {<span class=\"hljs-attr\">...pageProps<\/span>} \/&gt;<\/span><\/span>;\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Our application should now look like this on <a href=\"http:\/\/localhost:3000\/\">http:\/\/localhost:3000\/<\/a>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cloudinary-marketing-res.cloudinary.com\/image\/upload\/c_limit,w_2000\/f_auto\/q_auto\/media_jams\/s_B3DBA9B9184620599DDED6657F725AD5A2354C48CB802860DA95A5589848DE07_1630413074982_Screenshot+2021-08-31+at+13.29.40.png\" alt=\"\" loading=\"lazy\" class=\"c-transformed-asset\"  width=\"2000\" height=\"1118\"\/><\/p>\n<h2>Product Image Zoom<\/h2>\n<p>In the project, we install the <a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">react-image-magnify<\/a> package using the command below:<\/p>\n<pre class=\"js-syntax-highlighted\"><span><code class=\"hljs shcb-wrap-lines\">    npm i react-image-magnify\n<\/code><\/span><\/pre>\n<p>Next, in <code>pages\/index.js<\/code> we import <a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">react-image-magnify<\/a>.<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> ReactImageMagnify <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-image-magnify\"<\/span>;\n    \n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">IndexPage<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"body\"<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"image\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ReactImageMagnify<\/span>\n              {<span class=\"hljs-attr\">...<\/span>{\n                <span class=\"hljs-attr\">smallImage:<\/span> {\n                  <span class=\"hljs-attr\">alt:<\/span> \"\",\n                  <span class=\"hljs-attr\">isFluidWidth:<\/span> <span class=\"hljs-attr\">true<\/span>,\n                  <span class=\"hljs-attr\">src:<\/span> \"\",\n                  <span class=\"hljs-attr\">srcSet:<\/span> \"\",\n                  <span class=\"hljs-attr\">sizes:<\/span>\n                    \"(<span class=\"hljs-attr\">min-width:<\/span> <span class=\"hljs-attr\">1000px<\/span>) <span class=\"hljs-attr\">33.5vw<\/span>, (<span class=\"hljs-attr\">min-width:<\/span> <span class=\"hljs-attr\">415px<\/span>) <span class=\"hljs-attr\">50vw<\/span>, <span class=\"hljs-attr\">100vw<\/span>\"\n                },\n                <span class=\"hljs-attr\">largeImage:<\/span> {\n                  <span class=\"hljs-attr\">alt:<\/span> \"\",\n                  <span class=\"hljs-attr\">src:<\/span> \"\",\n                  <span class=\"hljs-attr\">width:<\/span> <span class=\"hljs-attr\">1200<\/span>,\n                  <span class=\"hljs-attr\">height:<\/span> <span class=\"hljs-attr\">1800<\/span>\n                },\n                <span class=\"hljs-attr\">isHintEnabled:<\/span> <span class=\"hljs-attr\">true<\/span>\n              }}\n            \/&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n          <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"text\"<\/span>&gt;<\/span>\n            \/\/ ...\n          <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We have successfully imported <a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">react-image-magnify<\/a> with the default configuration from the docs.<\/p>\n<p>Notice that we are yet to specify <code>src<\/code>, <code>srcSet<\/code>, and <code>alt<\/code> in the above snippet, to do that we will be using an image URL for <code>src<\/code> and we will also create an array to specify sizes that will be used for <code>srcSet<\/code>.<\/p>\n<p><code>src<\/code> : The src stands for image source, which is used to specify the source of an image in a tag.<\/p>\n<p><code>srcSet<\/code> : This is a new attribute that allows you to specify different images for different screen sizes\/orientations\/display-types.<\/p>\n<p><code>alt<\/code> : Adds a text description to an image on a Web page.<\/p>\n<p>Let\u2019s create our base URL and sizes with the snippet below:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> ReactImageMagnify <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-image-magnify\"<\/span>;\n    \n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">IndexPage<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    \n      <span class=\"hljs-keyword\">const<\/span> imageBaseUrl =\n        <span class=\"hljs-string\">\"https:\/\/res.cloudinary.com\/olanetsoft\/image\/upload\/cat.jpg\"<\/span>;\n    \n     <span class=\"hljs-keyword\">const<\/span> sizes = &#91; <span class=\"hljs-string\">\"355\"<\/span>,<span class=\"hljs-string\">\"481\"<\/span>,<span class=\"hljs-string\">\"584\"<\/span>,<span class=\"hljs-string\">\"687\"<\/span>,<span class=\"hljs-string\">\"770\"<\/span>,<span class=\"hljs-string\">\"861\"<\/span>,<span class=\"hljs-string\">\"955\"<\/span>,<span class=\"hljs-string\">\"1033\"<\/span>,<span class=\"hljs-string\">\"1112\"<\/span>,<span class=\"hljs-string\">\"1192\"<\/span>,<span class=\"hljs-string\">\"1200\"<\/span>];\n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"hljs-comment\">\/\/ ...<\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We defined our preferred picture sizes and created our primary image URL, which will be used for <code>srcSet<\/code> in the code snippet above.<\/p>\n<p>We will loop through all of the sizes in the array because <code>srcSet<\/code> is used to define various types of pictures for various screen sizes.<\/p>\n<p>Using the code snippet below, create the <code>srcSet<\/code>:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">    <span class=\"hljs-keyword\">import<\/span> ReactImageMagnify <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react-image-magnify\"<\/span>;\n    \n    <span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">IndexPage<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n      <span class=\"hljs-comment\">\/\/...<\/span>\n    \n    <span class=\"hljs-keyword\">const<\/span> srcSet = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n      sizes.forEach(<span class=\"hljs-function\">(<span class=\"hljs-params\">i<\/span>) =&gt;<\/span> {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">`https:\/\/res.cloudinary.com\/olanetsoft\/image\/upload\/w_<span class=\"hljs-subst\">${i}<\/span>,c_scale\/cat.jpg`<\/span>;\n        });\n    };\n    \n      <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"hljs-comment\">\/\/ ...<\/span>\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>We successfully iterated over the sizes array, passing it in our base URL to set the image size based on the sizes in the array. With this, we\u2019ll use the <code>srcSet<\/code> attributes to define different types of images.<\/p>\n<p>We will update the <code>src<\/code>, <code>srcSet<\/code>, and <code>alt<\/code> attribute as shown in the code snippet below:<\/p>\n<pre class=\"js-syntax-highlighted\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">    import ReactImageMagnify from <span class=\"hljs-string\">\"react-image-magnify\"<\/span>;\n    \n    export <span class=\"hljs-keyword\">default<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">IndexPage<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n      &#91;...]\n      <span class=\"hljs-keyword\">return<\/span> (\n        &lt;div className=<span class=\"hljs-string\">\"body\"<\/span>&gt;\n          &lt;div className=<span class=\"hljs-string\">\"image\"<\/span>&gt;\n            &lt;ReactImageMagnify\n              {...{\n                smallImage: {\n                  alt: <span class=\"hljs-string\">\"Cat\"<\/span>, <span class=\"hljs-comment\">\/\/ updated<\/span>\n                  isFluidWidth: <span class=\"hljs-keyword\">true<\/span>,\n                  src: `${imageBaseUrl}`, <span class=\"hljs-comment\">\/\/ updated<\/span>\n                  srcSet, <span class=\"hljs-comment\">\/\/ updated<\/span>\n                  sizes:\n                    <span class=\"hljs-string\">\"(min-width: 1000px) 33.5vw, (min-width: 415px) 50vw, 100vw\"<\/span>\n                },\n                largeImage: {\n                  alt: <span class=\"hljs-string\">\"Cat\"<\/span>, <span class=\"hljs-comment\">\/\/ updated<\/span>\n                  src: `${imageBaseUrl}`, <span class=\"hljs-comment\">\/\/ updated<\/span>\n                  width: <span class=\"hljs-number\">1200<\/span>,\n                  height: <span class=\"hljs-number\">1800<\/span>\n                },\n                isHintEnabled: <span class=\"hljs-keyword\">true<\/span>\n              }}\n            \/&gt;\n          &lt;\/div&gt;\n          &lt;div className=<span class=\"hljs-string\">\"text\"<\/span>&gt;\n            <span class=\"hljs-comment\">\/\/ ..<\/span>\n          &lt;\/div&gt;\n        &lt;\/div&gt;\n      );\n    }\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>The final interface should appear something like this, with the product image on the left and the text description on the right. See demo below:<\/p>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1cMHpmk5ii9pI4qdsASef4lP4KfyvkQFZ\/view?usp=drivesdk\">https:\/\/drive.google.com\/file\/d\/1cMHpmk5ii9pI4qdsASef4lP4KfyvkQFZ\/view?usp=drivesdk<\/a><\/p>\n<h1>Summary<\/h1>\n<!-- You forgot the \"y\" in Summary -->\n<p>This article explains how to create an E-commerce product image zoom lens in Next.js, using React-image-magnify.<\/p>\n<!-- Very fun build, several comments left to look over. Please triple-check over the embedded codesandbox piece. Thanks for the awesome content! -->\n<h1>Resources<\/h1>\n<p>You may find this resource useful.<\/p>\n<ul>\n<li>\n<a href=\"https:\/\/www.npmjs.com\/package\/react-image-magnify\">react-image-magnify<\/a>\n<\/li>\n<\/ul>\n<\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":41,"featured_media":28426,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[134,370,212,246,371],"class_list":["post-28425","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-guest-post","tag-image","tag-next-js","tag-react","tag-under-review"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Build a Product Image Zoom Lens in Next.js<\/title>\n<meta name=\"description\" content=\"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Build a Product Image Zoom Lens in Next.js\" \/>\n<meta property=\"og:description\" content=\"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-17T22:31:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-09T22:22:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae-jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"3999\" \/>\n\t<meta property=\"og:image:height\" content=\"2666\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Build a Product Image Zoom Lens in Next.js\",\"datePublished\":\"2021-11-17T22:31:57+00:00\",\"dateModified\":\"2025-02-09T22:22:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\"},\"wordCount\":9,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA\",\"keywords\":[\"Guest Post\",\"Image\",\"Next.js\",\"React\",\"Under Review\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2021\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\",\"name\":\"Build a Product Image Zoom Lens in Next.js\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA\",\"datePublished\":\"2021-11-17T22:31:57+00:00\",\"dateModified\":\"2025-02-09T22:22:21+00:00\",\"description\":\"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA\",\"width\":3999,\"height\":2666},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Build a Product Image Zoom Lens in Next.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Build a Product Image Zoom Lens in Next.js","description":"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/","og_locale":"en_US","og_type":"article","og_title":"Build a Product Image Zoom Lens in Next.js","og_description":"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js","og_url":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/","og_site_name":"Cloudinary Blog","article_published_time":"2021-11-17T22:31:57+00:00","article_modified_time":"2025-02-09T22:22:21+00:00","og_image":[{"width":3999,"height":2666,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae-jpg?_i=AA","type":"image\/jpeg"}],"twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/"},"author":{"name":"","@id":""},"headline":"Build a Product Image Zoom Lens in Next.js","datePublished":"2021-11-17T22:31:57+00:00","dateModified":"2025-02-09T22:22:21+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/"},"wordCount":9,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA","keywords":["Guest Post","Image","Next.js","React","Under Review"],"inLanguage":"en-US","copyrightYear":"2021","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/","url":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/","name":"Build a Product Image Zoom Lens in Next.js","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA","datePublished":"2021-11-17T22:31:57+00:00","dateModified":"2025-02-09T22:22:21+00:00","description":"Build the familiar image zoom lens in ecommerce products using React image magnify and Next.js","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA","width":3999,"height":2666},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/guest_post\/build-an-e-commerce-product-image-zoom-lens-in-next-js\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Build a Product Image Zoom Lens in Next.js"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":""}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1681924531\/Web_Assets\/blog\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae\/e9aa9277be192a4b14430f1d7081cf4d18ae4bf1-3999x2666-1_28426a5aae.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=28425"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28425\/revisions"}],"predecessor-version":[{"id":36785,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/28425\/revisions\/36785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/28426"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=28425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=28425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=28425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}