19
loading...
This website collects cookies to deliver better user experience
src
folder - Images stored in this directory will be included in the final bundle. Additionally, they have the added bonus of being able to be imported as a JavaScript module.public
folder - Files in public
directory will not be processed by Webpack. And they won't be a part of the final bundle.src
directory is a good solution and it is oftentimes encouraged to use instead of the static file serving. Most react developers tend to store their images in src/assets
folder. In React, you can import images as a data property that's stored in a JavaScript object. These data properties are accessible anywhere in your component.src
attribute of an img
tag so that it is not set until the image is visible in the browsersrc
attribute of an img
should be set to a given value. In most cases, this rule is set to an image entering the browser's viewport.react-lazyload
the images get loaded only when they come to the user's eye. It works by listening to the window scroll event and checking if any new image has appeared in the browser viewport. The library makes sure that an image is visible before it loads it in order to avoid unnecessary pixelation or unwanted memory usage.img
with LazyLoad
component and everything works out of the box.import React from "react";
import LazyLoad from "react-lazyload";
import image from "./image.jpg";
const App = () => {
/**
* Lazy loading images is supported out of box, no extra config is needed
* `height` is set for better experience
*/
return (
<LazyLoad height={200}>
<img src={image} />
</LazyLoad>
);
};
export default App;
src
and placeholder
properties.import React from "react";
import ProgressiveImage from "react-progressive-image";
import image from "./image.jpg";
import placeholderImage from "./placeholderImage.jpg";
const App = () => {
/**
* `placeholder` will be displayed
* while the original `src` image is being loaded
*/
return (
<ProgressiveImage src={image} placeholder={placeholderImage}>
{(src) => <img src={src} alt="an image" />}
</ProgressiveImage>
);
};
export default App;
placeholder
which is in most cases a very tiny version of an original image. The browser will load it much faster than the original image. However, we still need to load the placeholder from the server. To save us this trouble, we can directly use the base64 encoded image as the placeholder
.import React from "react";
import ProgressiveImage from "react-progressive-image";
import image from "./image.jpg";
import placeholderImage from "./placeholderImage.jpg";
const App = () => {
return (
<ProgressiveImage src={image} placeholder="data:image/png;base64***">
{(src) => <img src={src} alt="an image" />}
</ProgressiveImage>
);
};
export default App;
prop
. And returns img
element with the src
set to the given image.import React from "react";
const Image = ({ name }) => {
try {
// Import image on demand
const image = require(`assets/${name}`);
// If the image doesn't exist. return null
if (!image) return null;
return <img src={image.default} />;
} catch (error) {
console.log(`Image with name "${name}" does not exist`);
return null;
}
};
export default Image;
catch
block. It's a good idea to show a warning, so people using this component see something is not in order.