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

@rescui/use-resize-observer

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rescui/use-resize-observer

ResizeObserver hook

  • 0.2.1-RUI-198-Support-different-hardness-for-headings-in-mixins-api-5a735219.42
  • branch-RUI-198-Support-different-hardness-for-headings-in-mixins-api
  • npm
  • Socket score

Version published
Weekly downloads
2.3K
increased by36.65%
Maintainers
0
Weekly downloads
 
Created
Source

ResizeObserver hook

This package is used for observing react elements. It uses ResizeObserver and alleviates some of its bugs.

It's best used together with React refs. It can be used with raw HTML elements, although it's better to avoid that.

Usage examples

React ref example

import React, { useRef } from 'react';
import { useResizeObserver } from '@rescui/use-resize-observer';

const MyComponent = () => {
  const ref = useRef<HTMLElement>(null);
  useResizeObserver(ref, (entry: ResizeObserverEntry) => {
    // resize logic
  });

  return <div ref={ref}>To the moon!</div>;
};

HTML element example

Note: Avoid this option when possible due to additional re-renders when using callback refs and useState. Use React ref instead.

import React, { useState } from 'react';
import { useResizeObserver } from '@rescui/use-resize-observer';

const MyComponent = () => {
  const [el, setEl] = useState<HTMLElement>(null);
  useResizeObserver(el, (entry: ResizeObserverEntry) => {
    // resize logic
  });

  return <div ref={setEl}>To the moon!</div>;
};

Ignore first call

Sometimes you need to observe only changes and ignore the first observer call.

In that case, you may use the ignoreFirstCall option:

import React, { useState } from 'react';
import { useResizeObserver } from '@rescui/use-resize-observer';

const MyComponent = () => {
  const ref = useRef<HTMLElement>(null);
  useResizeObserver(
    ref,
    (entry: ResizeObserverEntry) => {
      // resize logic
    },
    { ignoreFirstCall: true }
  );

  return <div ref={ref}>To the moon!</div>;
};

FAQs

Package last updated on 21 Oct 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