![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
react-responsive-picture
Advanced tools
A future-proof responsive image component that supports latest Picture specification
A future-proof responsive image component that supports latest <picture>
specification. Uses picturefill for backward compatibility from IE9+.
npm install react-responsive-picture
or yarn add react-responsive-picture
react-responsive-picture
requires glamor
installed as peer dependency since version 2.0.0
so you need to add it (in case you're not using it in your project) by running:
npm install glamor
or yarn add glamor
import { Picture } from 'react-responsive-picture';
class App extends Component {
render() {
return (
<Picture
sources = {[
{
srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
media: "(max-width: 420px)",
},
{
srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
},
{
srcSet: "path-to-desktop-image.webp",
type: "image/webp"
}
]}
/>
);
};
}
Prop | Type | Default | Definition |
---|---|---|---|
sources | array | The array of source objects. Check Sources section for more information. | |
src | string | (transparent pixel) | Source for standalone/fallback image. To prevent issues in some browsers, by default src is set to a 1x1 transparent pixel data image. |
sizes | string | Sizes attribute to be used with src for determing best image for user's viewport. | |
alt | string | Alternative text for image | |
className | string | Any additional CSS classes you might want to use to style the image | |
css | object || array || string | Any additional styles you might want to send to the wrapper. Uses glamorous to process it so you can send an object, an array or even glamor generated string classes. |
Note: Before version 2.0.0
the style
prop was parsed by glamor
so it wasn't having the same behaviour as applying the style
prop to any other React component. For that reason, the recommended prop to override the styles is now css
, which will be parsed by glamorous
and applied to the component. The style
prop will be treated as inline styles so it still works, but you can't have the nice features from glamor
like hover states or media queries so be very careful about using it.
Normal <img>
like behaviour. The same image is displayed on every device/viewport.
<Picture src="path-to-image.jpg" />
will render:
<img srcset="path-to-image.jpg" />
Different images for specific devices (usually retina).
<Picture src="path-to-image@2x.png 2x, path-to-image.png 1x" />
will render:
<img srcset="path-to-image@2x.png 2x, path-to-image.png 1x" />
When you want to let the browser determine the best image for user's current viewport. More information about size
attribute on this great blog post.
<Picture
src="large.jpg 1024w, medium.jpg 640w, small.jpg 320w"
sizes="(min-width: 36em) 33.3vw, 100vw"
/>
will render:
<img srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w" sizes="(min-width: 36em) 33.3vw, 100vw" />
When you want to explicitly control which image is displayed at specific viewport sizes.
<Picture
sources = {[
{
srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
media: "(max-width: 420px)",
},
{
srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
},
{
srcSet: "path-to-desktop-image.webp",
type: "image/webp"
}
]}
/>
will render:
<picture>
<source srcset="path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x" media="(max-width: 420px)">
<source srcset="path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x">
<source srcset="path-to-desktop-image.webp" type="image/webp">
<img srcset="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />
</picture>
The sources
prop is where you can determine the behaviour of the <Picture>
component and which images will show for the specific screens.
For each source you can send an object containing srcSet
, media
and type
although the last two are optional.
There's also a <FullsizePicture>
component that you can use to display full-size images using the same benefits of <Picture>
for art direction.
<div style={{ height: 200 }}>
<FullsizePicture
sources = {[
{
srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x",
media: "(max-width: 420px)",
},
{
srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x",
},
]}
/>
</div>
It will automatically fill the entire parent element maintaining the original image ratio. Please note that the parent element needs to have a defined height as you would expect for any background image as well.
FullsizePicture
accepts the same props as Picture
plus a few more for styling.
Prop | Type | Default | Definition |
---|---|---|---|
pictureClassName | string | Any additional CSS classes you might want to use to style the inner Picture component | |
pictureCSS | object || array || string | Any additional styles you might want to send to the inner Picture component |
Please follow our contributing guidelines.
FAQs
A future-proof responsive image component that supports latest Picture specification
The npm package react-responsive-picture receives a total of 0 weekly downloads. As such, react-responsive-picture popularity was classified as not popular.
We found that react-responsive-picture demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.