Registry / web-framework / react-responsive-masonry

react-responsive-masonry

JSON →
library2.7.2jsnpmunverified

react-responsive-masonry is a lightweight React component library for creating responsive masonry layouts using CSS Flexbox. It provides two main components: `Masonry` for a static column count and `ResponsiveMasonry` for dynamically adjusting columns and gutters based on breakpoints. The current stable version is 2.7.2. Releases appear to be driven by bug fixes and minor feature enhancements rather than a strict cadence, with the last major update to v2.0.0 in 2020 introducing ES6 modules. Its key differentiator is its simplicity and reliance on flexbox, avoiding complex JavaScript layout algorithms for performance and ease of use, making it suitable for image galleries or card layouts where item order isn't strictly sequential.

npm install react-responsive-masonry
INSTALL
IMPORT
SIG · REACT-RESPONSIVE-M
R
react-responsive-masonry
web-frameworkjavascriptv2.7.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Masonry
import Masonry from 'react-responsive-masonry'
import { Masonry } from 'react-responsive-masonry'
`Masonry` is a default export, while `ResponsiveMasonry` is a named export. This is a common source of confusion.
ResponsiveMasonry
import { ResponsiveMasonry } from 'react-responsive-masonry'
import ResponsiveMasonry from 'react-responsive-masonry'
`ResponsiveMasonry` is a named export, while `Masonry` is a default export.
Masonry, ResponsiveMasonry
import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry'
const { Masonry, ResponsiveMasonry } = require('react-responsive-masonry');
While v2.0.0 added CommonJS build, the primary usage pattern for React components typically leverages ESM syntax. The component is also available via UMD for direct script inclusion.

This quickstart demonstrates how to set up a responsive masonry layout using both `ResponsiveMasonry` and `Masonry` components, showing how columns and gutters adapt to different screen sizes.

import React from 'react'; import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry'; // Imagine these are your child components or elements const ChildA = () => <div style={{ height: '100px', background: '#e0e0e0', margin: '5px' }}>Item A</div>; const ChildB = () => <div style={{ height: '150px', background: '#d0d0d0', margin: '5px' }}>Item B</div>; const ChildC = () => <div style={{ height: '80px', background: '#c0c0c0', margin: '5px' }}>Item C</div>; const ChildD = () => <div style={{ height: '200px', background: '#b0b0b0', margin: '5px' }}>Item D</div>; const ChildE = () => <div style={{ height: '120px', background: '#a0a0a0', margin: '5px' }}>Item E</div>; const ChildF = () => <div style={{ height: '90px', background: '#909090', margin: '5px' }}>Item F</div>; function App() { return ( <div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}> <h1>Responsive Masonry Example</h1> <ResponsiveMasonry columnsCountBreakPoints={{ 350: 1, 750: 2, 900: 3, 1200: 4 }} gutterBreakPoints={{ 350: "10px", 750: "15px", 900: "20px", 1200: "25px" }} > <Masonry gutter="10px"> <ChildA /> <ChildB /> <ChildC /> <ChildD /> <ChildE /> <ChildF /> {/* More children here */} </Masonry> </ResponsiveMasonry> </div> ); } export default App;
Debug
Known issues
breakingVersion 2.0.0 restructured the package to include separate ES6, CommonJS, and UMD builds. While existing import paths *should* mostly work due to `package.json` configurations, developers might experience issues if their build tools do not correctly resolve the new module entry points, especially in older environments.
fix
Ensure your bundler (Webpack, Rollup, Parcel) is configured to handle `module` and `main` fields in `package.json` correctly. If issues persist, try explicitly configuring aliases or adjusting import paths if applicable, though this is rarely necessary.
affects: >=2.0.0
gotchaThe `Masonry` component is a default export, while `ResponsiveMasonry` is a named export. Misunderstanding this can lead to 'is not a function' or 'undefined' errors.
fix
Always import `Masonry` as a default export (`import Masonry from '...'`) and `ResponsiveMasonry` as a named export (`import { ResponsiveMasonry } from '...'`). For both, use `import Masonry, { ResponsiveMasonry } from '...'`.
affects: >=1.0.0
breakingPrior to v1.2.0, `prop-types` was not explicitly listed as a peer dependency. In v1.2.0, `prop-types` was added to `peerDependencies`, and `react` was updated to `15.5`. If using an older React version (pre-15.5) or not having `prop-types` installed, you might encounter warnings or runtime errors related to prop validation.
fix
Ensure `prop-types` is installed (`npm install prop-types`) and your React version is 15.5 or higher. For React v16+, `prop-types` is still required as a separate package.
affects: >=1.2.0 <16.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'length')
This often occurs when `Masonry` is rendered without any children or its children prop is not an array-like structure.
fix
Ensure `Masonry` always receives valid React children (e.g., `<Masonry>{items.map(item => <div key={item.id}>{item.content}</div>)}</Masonry>`) or handles the empty state gracefully.
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
This error typically indicates an incorrect import for `Masonry` or `ResponsiveMasonry` (e.g., trying to use a named import for `Masonry` which is a default export).
fix
Check your import statements. `Masonry` should be imported as a default, and `ResponsiveMasonry` as a named import. Example: `import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry'`.
Warning: Failed prop type: Invalid prop `children` of type `array` supplied to `Masonry`, expected a single React element.
This warning, though uncommon for this specific library, could arise if React's development mode mistakenly applies a stricter `children` propType check or if the library's internal `children` handling expects a specific format.
fix
While `Masonry` is designed for multiple children, if this specific error appears (which might indicate an unusual environment or library conflict), ensure children are wrapped in a fragment or array explicitly if required, though typically direct children are passed. Double-check your React version compatibility and ensure no other libraries are interfering with prop type validation.
Upgrade
Version history
2.7.2latest on npm
Audit
Dependencies
reactrequiredPeer dependency for React applications. Updated to v16 in v1.4.0.
prop-typesoptionalPeer dependency for prop validation, added in v1.2.0.
Agent activity
4 hits · last 30 days
node
4
Resources
react-responsive-masonry — npm install react-responsive-masonry · libregistry