`react-loader-spinner` is a popular and versatile React library that provides a wide range of customizable loading spinners. These spinners are crucial for improving the user experience in web applications by providing visual feedback during asynchronous operations such as data fetching, component mounting, or any task that takes a noticeable amount of time. Instead of users seeing a blank screen or a frozen UI, a loader spinner indicates that the application is busy processing, thereby reducing perceived wait times and preventing user frustration.
Key features and benefits of `react-loader-spinner` include:
* Variety of Loaders: It offers a rich collection of diverse spinner types, from simple dots and rings to more complex animations like ball triangles, circles, and many more, catering to different design preferences.
* Ease of Use: Integrating a spinner is straightforward. After installation, you simply import the desired spinner component and render it conditionally in your React component.
* Customization: Each spinner component comes with various props that allow extensive customization. You can easily change the color, size, speed, animation duration, and even add custom class names or styles to match your application's theme.
* Performance: The library is designed to be lightweight, ensuring that adding loaders doesn't negatively impact the performance of your React application.
* Accessibility: Many loaders include `ariaLabel` props to improve accessibility for screen readers.
Installation:
To install `react-loader-spinner`, you typically use npm or yarn:
`npm install react-loader-spinner`
or
`yarn add react-loader-spinner`
Usage:
Once installed, you can import specific loader components from the library and render them based on a loading state. For instance, you would usually maintain a `isLoading` state variable in your component, and when `isLoading` is true, you render the spinner; otherwise, you render the actual content. This conditional rendering is fundamental to its usage. Each loader component accepts various props for customization like `height`, `width`, `color`, `visible`, etc., allowing you to tailor its appearance to fit your application's design system.
Example Code
import React, { useState, useEffect } from 'react';
import { ThreeDots, BallTriangle, MutatingDots } from 'react-loader-spinner';
// For newer versions (v5+), global CSS import is generally not needed as styles are inline or component-specific.
function DataLoader() {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
// Simulate an asynchronous data fetch
const fetchData = async () => {
try {
setLoading(true);
setError(null);
// Simulate network delay
await new Promise(resolve => setTimeout(resolve, 3000));
const responseData = { message: "Data successfully loaded from API!" };
setData(responseData);
} catch (err) {
setError("Failed to fetch data.");
console.error("Error fetching data:", err);
} finally {
setLoading(false);
}
};
fetchData();
}, []);
return (
<div style={{ padding: '20px', textAlign: 'center' }}>
<h1>React Loader Spinner Example</h1>
{loading ? (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', marginTop: '50px' }}>
{/* Example 1: ThreeDots Loader */}
<p>Loading content with ThreeDots...</p>
<ThreeDots
visible={true}
height="80"
width="80"
color="#4fa94d"
radius="9"
ariaLabel="three-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
<p style={{ marginTop: '30px' }}>Loading content with BallTriangle...</p>
{/* Example 2: BallTriangle Loader with custom color and speed */}
<BallTriangle
height={100}
width={100}
radius={5}
color="#00BFFF"
ariaLabel="ball-triangle-loading"
wrapperClass=""
wrapperStyle={{}}
visible={true}
/>
<p style={{ marginTop: '30px' }}>Loading content with MutatingDots...</p>
{/* Example 3: MutatingDots Loader */}
<MutatingDots
visible={true}
height="100"
width="100"
color="#a742f5"
secondaryColor="#f0c23a"
radius="12.5"
ariaLabel="mutating-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
) : error ? (
<div style={{ color: 'red', marginTop: '50px' }}>
<p>{error}</p>
</div>
) : (
<div style={{ marginTop: '50px', border: '1px solid #ccc', padding: '20px', borderRadius: '8px' }}>
<h2>Data Loaded!</h2>
<p>{data.message}</p>
<p>This is where your actual application content would go.</p>
</div>
)}
</div>
);
}
export default DataLoader;








react-loader-spinner