ResponsiveImage

ResponsiveImage

If you need an image that needs to be a responsive image and you want a react component to generate the srcset for this image you can use the ResponsiveImage component.

Its props are as follows:

PropTypeDescription
srcstringThis is the src for the image
altstringThis is the alt attribute for your image
srcSetstringThis is an optional string you can pass in if you want to add a custom srcset
classNamestringThis allows you to add custom styles to the component

Usage

If you wanted an image to have the default srcset, 175w to 1920w you would use the component like this:

caution

Your src prop must be a string that contains _x_, the component will still work even if your string doesn't contain _x_ however it will not have a a working srcset.

import React from 'react';
import { ResponsiveImage } from 'homeflowjs/images';
const ExampleComponent = () => {
const exampleImage = 'https://mr1.homeflow-assets.co.uk/files/site_asset/image/4743/3745/_x_/placeholder-house.jpg';
return (
<ResponsiveImage
className="example-class-name"
src={exampleImage}
alt="Example image"
/>
)
}

replaceImageDimensions

The replaceImageDimensions function allows you to replace the _x_ with a width, height or a width and height value. It takes three arguments, src, width and height. You can use this function when passing in your own custom srcset to the ResponsiveImage component for example:

import { ResponsiveImage, replaceImageDimensions } from 'homeflowjs/images';
const ExampleComponent = () => {
const exampleImage = 'https://mr1.homeflow-assets.co.uk/files/site_asset/image/4743/3745/_x_/placeholder-house.jpg';
const customSrcSet =
`${replaceImageDimensions(src, 200, 200)} 200w,
${replaceImageDimensions(src, 400)} 400w,`;
return (
<ResponsiveImage
className="example-class-name"
src={exampleImage}
alt="Example image"
srcSet={customSrcSe}
/>
)
}