
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@react-hook/resize-observer
Advanced tools
A React hook that fires a callback whenever ResizeObserver detects a change to its size
@react-hook/resize-observer is a React hook that allows you to observe changes to the size of an element. It leverages the ResizeObserver API to provide a simple and efficient way to handle resize events in your React components.
Basic Resize Observer
This example demonstrates how to use the `useResizeObserver` hook to observe changes to the size of a `div` element. When the size changes, the new dimensions are logged to the console.
```jsx
import React, { useRef } from 'react';
import useResizeObserver from '@react-hook/resize-observer';
const ResizeComponent = () => {
const ref = useRef(null);
useResizeObserver(ref, (entry) => {
console.log('Size changed:', entry.contentRect);
});
return <div ref={ref} style={{ width: '100%', height: '100%' }}>Resize me!</div>;
};
export default ResizeComponent;
```
Handling Resize Events
This example shows how to use the `useResizeObserver` hook to update the component's state with the new dimensions whenever the observed element is resized. The dimensions are then displayed within the `div`.
```jsx
import React, { useRef, useState } from 'react';
import useResizeObserver from '@react-hook/resize-observer';
const ResizeComponent = () => {
const ref = useRef(null);
const [size, setSize] = useState({ width: 0, height: 0 });
useResizeObserver(ref, (entry) => {
setSize({
width: entry.contentRect.width,
height: entry.contentRect.height
});
});
return (
<div ref={ref} style={{ width: '100%', height: '100%' }}>
Width: {size.width}, Height: {size.height}
</div>
);
};
export default ResizeComponent;
```
react-resize-detector is a React component that provides a way to detect element resizes. It offers a higher-level API compared to @react-hook/resize-observer and includes additional features like debounce and throttle options.
react-use-measure is a React hook that combines the ResizeObserver and the IntersectionObserver APIs to provide a comprehensive solution for measuring elements. It offers more advanced features like margin and offset handling, making it more versatile than @react-hook/resize-observer.
react-resize-aware is a React hook that provides a simple way to handle resize events. It is similar to @react-hook/resize-observer but includes additional features like a built-in resize handle and support for both functional and class components.
npm i @react-hook/resize-observer
A React hook that fires a callback whenever ResizeObserver detects a change to its size.
ResizeObserver
for tracking all elements used by the hooks.
This approach is astoundingly more performant
than using a ResizeObserver
per element which most hook implementations do.useCallback()
because any mutations
are handled by the hook.Check out an example on CodeSandbox
import * as React from 'react'
import useResizeObserver from '@react-hook/resize-observer'
const useSize = (target) => {
const [size, setSize] = React.useState()
React.useLayoutEffect(() => {
setSize(target.current.getBoundingClientRect())
}, [target])
// Where the magic happens
useResizeObserver(target, (entry) => setSize(entry.contentRect))
return size
}
const App = () => {
const target = React.useRef(null)
const size = useSize(target)
return (
<pre ref={target}>
{JSON.stringify({width: size.width, height: size.height}, null, 2)}
</pre>
)
}
function useResizeObserver<T extends Element>(
target: React.RefObject<T> | React.ForwardedRef<T> | T | null,
callback: UseResizeObserverCallback,
options?: UseResizeObserverOptions
): ResizeObserver
Argument | Type | Required? | Description |
---|---|---|---|
target | React.RefObject | T | null | Yes | A React ref created by useRef() or an HTML element |
callback | UseResizeObserverCallback | Yes | Invoked with a single ResizeObserverEntry any time the target resizes |
options | UseResizeObserverOptions | No | Options for the ResizeObserver instance. |
export type UseResizeObserverCallback = (
entry: ResizeObserverEntry,
observer: ResizeObserver
) => any
export type UseResizeObserverOptions = {
polyfill?: any
}
polyfill
A ResizeObserver
polyfill implementation such as @juggle/resize-observer
(this was the default in V1.x).
import useResizeObserver from '@react-hook/resize-observer'
import {ResizeObserver} from '@juggle/resize-observer'
useResizeObserver(..., ..., { polyfill: ResizeObserver })
MIT
FAQs
A React hook that fires a callback whenever ResizeObserver detects a change to its size
We found that @react-hook/resize-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.