Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-resize-observer

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-resize-observer

A React hook that allows you to use a ResizeObserver to measure an element's size.

  • 9.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.5M
increased by2.93%
Maintainers
1
Weekly downloads
 
Created

What is use-resize-observer?

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.

What are use-resize-observer's main functionalities?

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>;
};

Other packages similar to use-resize-observer

Keywords

FAQs

Package last updated on 22 Nov 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc