React Spinners
React Spinners is a comprehensive collection of declarative, customizable loading spinner components designed for React applications. Currently stable at version 0.17.0, it offers a diverse range of animated loaders to provide visual feedback during asynchronous operations. The library maintains an active development and release cadence, consistently delivering updates, bug fixes, and performance improvements. A significant distinguishing feature, introduced in version 0.13.0, is its complete elimination of external runtime dependencies, drastically reducing bundle size and improving overall project footprint. It provides robust TypeScript support, integrates seamlessly with modern React ecosystems, supports React versions from 16 through 19, and is optimized for tree-shaking.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'barCount')
cause Attempting to use `ScaleLoader` from `react-spinners@0.16.0` without providing the required `barCount` prop.fixUpgrade to `react-spinners@0.16.1` or higher, or explicitly pass a `barCount` prop (e.g., `<ScaleLoader barCount={5} />`) if locked to `v0.16.0`. -
ReferenceError: require is not defined
cause Attempting to import `react-spinners` components using CommonJS `require()` syntax in an ES Module-only environment (e.g., a modern React app or a Next.js App Router component).fixUse ES Module named imports: `import { ClipLoader } from 'react-spinners';` instead of `const ClipLoader = require('react-spinners');`. -
Error: Hydration failed because the initial UI does not match what was rendered on the server.
cause Using `react-spinners` components directly in a React Server Component (RSC) without them being marked as client components, leading to a mismatch between server-rendered and client-hydrated content.fixUpgrade to `react-spinners@0.17.0` or higher, which includes necessary `"use client"` directives. For older versions, explicitly make the component a client component by adding `'use client';` at the top of its file, or render the spinner within a client-side wrapper component. -
All HashLoader components render with the same color, despite being passed different 'color' props.
cause This is a known bug specifically present in `react-spinners@0.14.0` and `react-spinners@0.14.1` related to `HashLoader`'s color management.fixDowngrade to `react-spinners@0.13.x` or upgrade to `react-spinners@0.15.0` or newer to resolve this `HashLoader` specific coloring issue.
Warnings
- breaking Version 0.16.0 introduced a breaking change where the `barCount` prop for `ScaleLoader` became mandatory. This was an unintended change.
- gotcha Prior to version 0.17.0, `react-spinners` components might not have functioned correctly or required manual client component directives when used directly within React Server Components (RSC) contexts.
- gotcha Version 0.17.0 removed UMD and duplicated CommonJS files from the published package to reduce its total size. While beneficial for most, users with highly specific or legacy CommonJS build configurations might encounter changes in module resolution or bundling behavior.
- gotcha A bug in `react-spinners` versions 0.14.0 and 0.14.1 caused multiple `HashLoader` instances with different `color` props to render with the same color, due to a fix being introduced and then immediately reverted.
- gotcha Version 0.13.0 underwent a major refactor, removing `@emotion/react` as a dependency and rewriting all components as functional components. This change drastically reduced the library's bundle size and removed an external styling engine dependency.
Install
-
npm install react-spinners -
yarn add react-spinners -
pnpm add react-spinners
Imports
- ClipLoader
const ClipLoader = require('react-spinners');import { ClipLoader } from 'react-spinners'; - ScaleLoader
import ScaleLoader from 'react-spinners';
import { ScaleLoader } from 'react-spinners'; - DotLoader
import { DotLoader, RingLoader } from 'react-spinners/dist/DotLoader';import { DotLoader } from 'react-spinners';
Quickstart
import { useState, CSSProperties } from "react";
import { ClipLoader } from "react-spinners";
const override: CSSProperties = {
display: "block",
margin: "0 auto",
borderColor: "red"
};
function App() {
let [loading, setLoading] = useState(true);
let [color, setColor] = useState("#ffffff");
return (
<div className="sweet-loading">
<button onClick={() => setLoading(!loading)}>Toggle Loader</button>
<input
value={color}
onChange={(e) => setColor(e.target.value)}
placeholder="Color of the loader"
/>
<ClipLoader
color={color}
loading={loading}
cssOverride={override}
size={150}
aria-label="Loading Spinner"
data-testid="loader"
/>
</div>
);
}
export default App;