Gathering detailed insights and metrics for react-resize-detector
Gathering detailed insights and metrics for react-resize-detector
Gathering detailed insights and metrics for react-resize-detector
Gathering detailed insights and metrics for react-resize-detector
@types/react-resize-detector
Stub TypeScript definitions entry for react-resize-detector, which provides its own types definitions
react-resize-detector-enhanced
React resize detector
@jameswomack/react-resize-detector
React resize detector
@pmbs/react-resize-detector
React resize detector
A Cross-Browser, Event-based, Element Resize Detection for React
npm install react-resize-detector
Typescript
Module System
Node Version
NPM Version
TypeScript (89.76%)
CSS (5.88%)
JavaScript (3.45%)
HTML (0.91%)
Total Downloads
394,871,076
Last Day
45,087
Last Week
1,152,739
Last Month
5,029,831
Last Year
65,855,070
MIT License
1,282 Stars
538 Commits
95 Forks
9 Watchers
5 Branches
37 Contributors
Updated on Jun 28, 2025
Latest Version
12.0.2
Package Id
react-resize-detector@12.0.2
Unpacked Size
61.86 kB
Size
11.21 kB
File Count
21
NPM Version
10.9.0
Node Version
22.11.0
Published on
Jan 01, 2025
Cumulative downloads
Total Downloads
Last Day
-9.8%
45,087
Compared to previous day
Last Week
-8.5%
1,152,739
Compared to previous week
Last Month
-3.2%
5,029,831
Compared to previous month
Last Year
-36.1%
65,855,070
Compared to previous year
Modern browsers now have native support for detecting element size changes through ResizeObservers. This library utilizes ResizeObservers to facilitate managing element size changes in React applications.
🐥 Tiny ~2kb
🐼 Written in TypeScript
🐠 Used by 170k repositories
🦄 Produces 100 million downloads annually
No window.resize
listeners! No timeouts!
Container queries now work in all major browsers. It's very likely you can solve your task using pure CSS.
1<div class="post"> 2 <div class="card"> 3 <h2>Card title</h2> 4 <p>Card content</p> 5 </div> 6</div>
1.post { 2 container-type: inline-size; 3} 4 5/* Default heading styles for the card title */ 6.card h2 { 7 font-size: 1em; 8} 9 10/* If the container is larger than 700px */ 11@container (min-width: 700px) { 12 .card h2 { 13 font-size: 2em; 14 } 15}
1npm i react-resize-detector 2// OR 3yarn add react-resize-detector
1import { useResizeDetector } from 'react-resize-detector'; 2 3const CustomComponent = () => { 4 const { width, height, ref } = useResizeDetector(); 5 return <div ref={ref}>{`${width}x${height}`}</div>; 6};
1import { useResizeDetector } from 'react-resize-detector'; 2 3const CustomComponent = () => { 4 const onResize = useCallback(() => { 5 // on resize logic 6 }, []); 7 8 const { width, height, ref } = useResizeDetector({ 9 handleHeight: false, 10 refreshMode: 'debounce', 11 refreshRate: 1000, 12 onResize, 13 }); 14 15 return <div ref={ref}>{`${width}x${height}`}</div>; 16};
It's not advised to use this approach, as dynamically mounting and unmounting the observed element could lead to unexpected behavior.
1import { useResizeDetector } from 'react-resize-detector'; 2 3const CustomComponent = () => { 4 const targetRef = useRef(); 5 const { width, height } = useResizeDetector({ targetRef }); 6 return <div ref={targetRef}>{`${width}x${height}`}</div>; 7};
Prop | Type | Description | Default |
---|---|---|---|
onResize | Func | Function that will be invoked with width , height and ResizeObserver entry arguments | undefined |
handleWidth | Bool | Trigger onResize on width change | true |
handleHeight | Bool | Trigger onResize on height change | true |
skipOnMount | Bool | Do not trigger onResize when a component mounts | false |
refreshMode | String | Possible values: throttle and debounce See lodash docs for more information. undefined - callback will be fired for every frame | undefined |
refreshRate | Number | Use this in conjunction with refreshMode . Important! It's a numeric prop so set it accordingly, e.g. refreshRate={500} | 1000 |
refreshOptions | Object | Use this in conjunction with refreshMode . An object in shape of { leading: bool, trailing: bool } . Please refer to lodash's docs for more info | undefined |
observerOptions | Object | These options will be used as a second parameter of resizeObserver.observe method. | undefined |
targetRef | Ref | Use this prop to pass a reference to the element you want to attach resize handlers to. It must be an instance of React.useRef or React.createRef functions | undefined |
Thanks to @Primajin for posting this snippet
1const { ResizeObserver } = window; 2 3beforeEach(() => { 4 delete window.ResizeObserver; 5 window.ResizeObserver = jest.fn().mockImplementation(() => ({ 6 observe: jest.fn(), 7 unobserve: jest.fn(), 8 disconnect: jest.fn(), 9 })); 10 11 wrapper = mount(<MyComponent />); 12}); 13 14afterEach(() => { 15 window.ResizeObserver = ResizeObserver; 16 jest.restoreAllMocks(); 17}); 18 19it('should do my test', () => { 20 // [...] 21});
MIT
Show us some love and STAR ⭐ the project if you find it useful
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
9 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 8
Reason
Found 5/7 approved changesets -- score normalized to 7
Reason
9 existing vulnerabilities detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More