Registry / web-framework / react-virtualized-auto-sizer

react-virtualized-auto-sizer

JSON →
library2.0.3jsnpmunverified

react-virtualized-auto-sizer is a specialized React component designed to automatically measure the available width and height of its parent HTMLElement and pass these dimensions as props to its child component. It leverages the modern ResizeObserver API for efficient and accurate dimension tracking. The package is currently at version 2.0.3 and is actively maintained, with recent updates focused on bug fixes and documentation. It originated as a fork of the `AutoSizer` component from `react-virtualized` and was initially intended to complement `react-window` for auto-sizing virtualized lists. However, it's important to note that more recent versions of `react-window` have integrated native ResizeObserver support, potentially making this package redundant for those specific use cases. Its key differentiators include precise sub-pixel measurements (using `getBoundingClientRect`), support for various ResizeObserver box models, and a flexible render prop API.

npm install react-virtualized-auto-sizer
INSTALL
IMPORT
SIG · REACT-VIRTUALIZED-
R
react-virtualized-auto-sizer
web-frameworkjavascriptv2.0.3
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.

AutoSizer
import { AutoSizer } from 'react-virtualized-auto-sizer';
import AutoSizer from 'react-virtualized-auto-sizer';
AutoSizer is a named export. Default import (`import AutoSizer from '...'`) will result in an undefined component in modern ESM/TypeScript setups.
Size
import type { Size } from 'react-virtualized-auto-sizer';
import { Size } from 'react-virtualized-auto-sizer';
When only importing the type for TypeScript, use `import type` to ensure it's removed during compilation, avoiding potential runtime issues or bundle size increase. The package ships its own types.
AutoSizerProps
import type { AutoSizerProps } from 'react-virtualized-auto-sizer';
import { AutoSizerProps } from 'react-virtualized-auto-sizer';
Importing component props for TypeScript definition should use `import type` for clarity and to prevent runtime import issues if not tree-shaken.

This quickstart demonstrates how to use `AutoSizer` to automatically adjust the dimensions of a child component based on its parent's available space, displaying the measured width and height.

import { AutoSizer } from 'react-virtualized-auto-sizer'; import React, { FC } from 'react'; import { createRoot } from 'react-dom/client'; interface MyContentProps { width: number; height: number; } // A simple child component that receives width and height props const MyContentComponent: FC<MyContentProps> = ({ width, height }) => { if (width === 0 || height === 0) { return <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}> <p>Parent has zero dimensions. Waiting for resize...</p> </div>; } return ( <div style={{ width, height, border: '1px solid #007bff', padding: '10px', overflow: 'auto' }}> <p>Content sized by AutoSizer:</p> <p>Width: {width.toFixed(2)}px</p> <p>Height: {height.toFixed(2)}px</p> <div style={{ height: 200, background: '#e0f7fa', margin: '10px 0' }}> Scrollable content placeholder. </div> <div style={{ height: 200, background: '#e0f7fa', margin: '10px 0' }}> More content to fill space. </div> </div> ); }; const App = () => ( <div style={{ display: 'flex', flexDirection: 'column', height: '100vh', padding: '16px', boxSizing: 'border-box' }}> <h1 style={{ marginBottom: '16px' }}>AutoSizer Basic Usage</h1> <p style={{ marginBottom: '16px' }}>Resize your browser window to see the AutoSizer adjust its child's dimensions dynamically.</p> <div style={{ flexGrow: 1, border: '2px dashed #6c757d', minHeight: '100px', display: 'flex' }}> <AutoSizer> {({ height, width }) => ( <MyContentComponent width={width} height={height} /> )} </AutoSizer> </div> </div> ); const container = document.getElementById('root'); if (container) { const root = createRoot(container); root.render(<App />); } else { console.error('Root element not found. Please ensure an element with id="root" exists in your HTML.'); }
Debug
Known issues
breakingVersion 2.0.0 introduced significant API changes, simplifying the component by removing `defaultHeight`, `defaultWidth`, `disableHeight`, and `disableWidth` props. The `children` prop no longer implicitly functions as a render prop.
fix
Refer to the migration guide for version 2.x. Use `renderProp` or `ChildComponent` props instead of `children` for passing a function. Default sizing should be handled by CSS or parent components, or by providing default values to the props received by the `renderProp` or `ChildComponent`.
affects: >=2.0.0
deprecatedThe `Child` prop, used to pass a child component, is deprecated since version 2.0.1.
fix
Use the `ChildComponent` prop for passing a React component or `renderProp` for passing a function that returns JSX. `renderProp` is generally recommended for its flexibility.
affects: >=2.0.1
gotchaMore recent versions of `react-window` now incorporate native `ResizeObserver` support and typically do not require `react-virtualized-auto-sizer`. Using this package with newer `react-window` might be redundant.
fix
Before integrating, check the documentation for your specific `react-window` version to determine if external auto-sizing is still necessary. You might be able to remove `react-virtualized-auto-sizer` for a smaller bundle size.
affects: *
gotchaThe `AutoSizer` component will pass `width` and `height` as `0` during its initial render, especially in server-side rendering (SSR) environments or if its parent has no defined dimensions.
fix
Ensure the parent element of `AutoSizer` has explicit CSS dimensions (e.g., `height: '100%'`, `width: '100%'`, fixed pixels). In SSR, handle the `0` values gracefully in your child component or consider using CSS to define a minimum size until hydration.
affects: *
Errors
Common errors & fixes
TypeError: (0, react_virtualized_auto_sizer__WEBPACK_IMPORTED_MODULE_3__.default) is not a function
Attempting to import `AutoSizer` as a default export (e.g., `import AutoSizer from '...'`) when it is a named export. This often happens after upgrading tooling or moving to a pure ESM environment.
fix
Change the import statement to use named imports: `import { AutoSizer } from 'react-virtualized-auto-sizer';`
Error: ResizeObserver loop limit exceeded
This browser warning occurs when a ResizeObserver cannot deliver all pending observations within a single animation frame, often due to a component rapidly changing its size in response to the `width`/`height` props received from `AutoSizer`, creating a feedback loop. This can also happen in development due to browser extensions or overlay tools.
fix
Ensure that the child component receiving `width` and `height` props from `AutoSizer` does not independently attempt to set its own dimensions in a way that conflicts with or triggers further resizes based on those props. Check for conflicting CSS properties like `box-sizing` or flexbox layouts where `AutoSizer` is a direct child of a flex container without an intermediate block element. This error is often benign and can sometimes be ignored in development.
The component wrapped by AutoSizer does not render, or renders with zero width/height.
The `AutoSizer` component needs its parent `HTMLElement` to have a defined width and height to measure. If the parent element's dimensions are not set (e.g., `height: 'auto'` on all ancestors), `AutoSizer` will report `0` for width and height.
fix
Ensure that the parent of your `AutoSizer` component has explicit dimensions set via CSS (e.g., `style={{ height: '100%', width: '100%' }}`) or is contained within another element that provides those dimensions.
TypeError: children is not a function
In version 2.x and later, the `children` prop is no longer implicitly treated as a render prop. The API was updated to separate `renderProp` and `ChildComponent`.
fix
Update your usage to explicitly use the `renderProp` for function-as-a-child pattern: `<AutoSizer renderProp={({ height, width }) => <MyComponent height={height} width={width} />} />`.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
reactrequiredPeer dependency required for rendering React components.
react-domrequiredPeer dependency required for rendering React components to the DOM.
Agent activity
6 hits · last 30 days
node
4
OpenAI (training)
2
Resources
react-virtualized-auto-sizer — npm install react-virtualized-auto-sizer · libregistry