React Loading Components
react-loading provides a collection of easy-to-use, customizable loading animations for React applications. It leverages SVG animations adapted from Brent Jackson's `loading` project, offering a variety of types such as 'balls', 'bars', 'bubbles', and 'spin'. The current stable version is 2.0.3. The package has a somewhat sporadic release cadence, with updates addressing bug fixes and minor enhancements, rather than frequent new features. Its primary differentiators include the diverse range of aesthetically pleasing SVG-based animations and straightforward prop-based customization for color, size, and delay, making it a convenient choice for integrating visual loading states into React UIs without external stylesheets or complex setup.
Common errors
-
Warning: Failed prop type: The prop `type` is marked as required in `ReactLoading`, but its value is `undefined`.
cause The `type` prop was not provided or was `undefined` when rendering the `ReactLoading` component.fixPass a valid string value for the `type` prop, chosen from the available loading types (e.g., 'balls', 'spin', 'bubbles'). -
Error: `prop-types` is a peer dependency of `react-loading`. Please ensure `prop-types` is installed in your project.
cause The `prop-types` package, which is a peer dependency of `react-loading`, is not installed in the project's `node_modules`.fixInstall `prop-types`: `npm install prop-types` or `yarn add prop-types`. -
Objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner, _store}). If you meant to render a collection of children, use an array instead.cause This error typically indicates that you're trying to render the `ReactLoading` component without correctly importing it as a default export, or a similar React element misuse, leading to `ReactLoading` itself being treated as a child.fixEnsure you are using `import ReactLoading from 'react-loading';` and rendering it as `<ReactLoading ... />` within your JSX, not as a function call or an incorrectly imported object.
Warnings
- breaking The default value for the `delay` prop changed from an undefined behavior to `0` milliseconds. If you relied on a previous implicit delay, you may need to explicitly set it now.
- gotcha The `height` and `width` props, previously primarily numeric (interpreted as pixels), now explicitly support string values (e.g., '20%', '100vw'). Ensure your values are correctly typed.
- gotcha This package relies on `react` and `prop-types` as peer dependencies. Missing these can lead to runtime errors or warnings in the console.
Install
-
npm install react-loading -
yarn add react-loading -
pnpm add react-loading
Imports
- ReactLoading
const ReactLoading = require('react-loading');import ReactLoading from 'react-loading';
Quickstart
import React from 'react';
import ReactLoading from 'react-loading';
const LoadingSpinner = ({ type, color, size = '64px' }) => {
// Example usage for different loading types and dynamic sizing
const loadingTypes = ['balls', 'spin', 'bubbles', 'cylon'];
const chosenType = type || loadingTypes[Math.floor(Math.random() * loadingTypes.length)];
return (
<div style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100vh',
backgroundColor: '#282c34'
}}>
<ReactLoading
type={chosenType}
color={color || '#fff'}
height={size}
width={size}
delay={0}
/>
</div>
);
};
export default LoadingSpinner;
// To use this component:
// <LoadingSpinner type="spin" color="#61dafb" size="100px" />
// <LoadingSpinner type="balls" color="#a0a0a0" />