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

@react-hook/resize-observer

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-hook/resize-observer

A React hook that fires a callback whenever ResizeObserver detects a change to its size

  • 2.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
258K
decreased by-29.45%
Maintainers
1
Weekly downloads
 
Created

What is @react-hook/resize-observer?

@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.

What are @react-hook/resize-observer's main functionalities?

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

Other packages similar to @react-hook/resize-observer

Keywords

FAQs

Package last updated on 30 Jul 2024

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