
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@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> | T | null,
callback: UseResizeObserverCallback
): 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
expo
## LICENSE
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 healthy version release cadence and project activity because the last version was released less than 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
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.