Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
use-resize-observer
Advanced tools
A React hook that allows you to use a ResizeObserver to measure an element's size.
The use-resize-observer package provides a React hook that allows you to monitor an element for size changes. It is useful for responsive design and element-specific adjustments based on width and height. It leverages the ResizeObserver API to offer a simple and efficient way to react to size changes in components.
Basic usage for monitoring size changes
This code demonstrates how to use the use-resize-observer hook to monitor the size of a component. By destructuring `ref`, `width`, and `height` from the hook, you can easily track the dimensions of the element that the `ref` is attached to.
import useResizeObserver from 'use-resize-observer';
const MyComponent = () => {
const { ref, width, height } = useResizeObserver();
return (
<div ref={ref}>
The width is {width} and the height is {height}.
</div>
);
};
Using with a callback
This example shows how to use the hook with a callback function. The `onResize` option allows you to provide a function that will be called whenever the observed element's size changes, receiving the new `width` and `height` as parameters.
import useResizeObserver from 'use-resize-observer';
const MyComponent = () => {
const { ref } = useResizeObserver({
onResize: ({ width, height }) => {
console.log(`New size: ${width}x${height}`);
}
});
return <div ref={ref}>Resize me and check the console!</div>;
};
react-resize-detector is another package that provides components and hooks to listen to resize events. Compared to use-resize-observer, it offers more customization options, such as the ability to choose between a hook or a higher-order component for detecting resize events. However, use-resize-observer might be simpler to use for straightforward use cases.
resize-observer-polyfill is not a React-specific package but a polyfill for the ResizeObserver API. It can be used in conjunction with React but requires manual setup and teardown of the observer. use-resize-observer abstracts this setup, offering a more integrated React hook approach.
A React hook that allows you to use a ResizeObserver to measure an element's size.
yarn add use-resize-observer
# or
npm install --save use-resize-observer
import React from "react";
import useResizeObserver from "use-resize-observer";
const App = () => {
const [ref, width, height] = useResizeObserver();
return (
<div ref={ref}>
Size: {width}x{height}
</div>
);
};
MIT
FAQs
A React hook that allows you to use a ResizeObserver to measure an element's size.
The npm package use-resize-observer receives a total of 860,137 weekly downloads. As such, use-resize-observer popularity was classified as popular.
We found that use-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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.