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

react-use-measure

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-measure

measure view bounds

  • 2.1.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
836K
decreased by-2.27%
Maintainers
2
Weekly downloads
 
Created

What is react-use-measure?

The react-use-measure package is a React hook that allows you to measure the size and position of a DOM element. It provides a simple and efficient way to get the dimensions and position of an element, which can be useful for various UI-related tasks such as responsive design, animations, and more.

What are react-use-measure's main functionalities?

Basic Measurement

This feature allows you to measure the dimensions and position of a DOM element. The `useMeasure` hook returns a ref to attach to the element you want to measure and an object containing the measurements.

import React from 'react';
import { useMeasure } from 'react-use-measure';

const Example = () => {
  const [ref, bounds] = useMeasure();

  return (
    <div>
      <div ref={ref} style={{ width: '50%', height: '100px', background: 'lightblue' }}>
        Measure me!
      </div>
      <pre>{JSON.stringify(bounds, null, 2)}</pre>
    </div>
  );
};

export default Example;

Responsive Measurement

This feature demonstrates how the `useMeasure` hook can be used to create responsive components that adapt to changes in their size and position. The measurements update automatically when the window is resized.

import React from 'react';
import { useMeasure } from 'react-use-measure';

const ResponsiveComponent = () => {
  const [ref, bounds] = useMeasure();

  return (
    <div>
      <div ref={ref} style={{ width: '100%', height: '200px', background: 'lightcoral' }}>
        Resize the window to see changes
      </div>
      <pre>{JSON.stringify(bounds, null, 2)}</pre>
    </div>
  );
};

export default ResponsiveComponent;

Conditional Rendering Based on Measurements

This feature shows how you can use the measurements obtained from `useMeasure` to conditionally render elements based on the size of the measured element.

import React from 'react';
import { useMeasure } from 'react-use-measure';

const ConditionalRendering = () => {
  const [ref, bounds] = useMeasure();

  return (
    <div>
      <div ref={ref} style={{ width: '100%', height: '150px', background: 'lightgreen' }}>
        Conditional Rendering
      </div>
      {bounds.width > 300 && <div>Width is greater than 300px</div>}
      {bounds.height > 100 && <div>Height is greater than 100px</div>}
    </div>
  );
};

export default ConditionalRendering;

Other packages similar to react-use-measure

Keywords

FAQs

Package last updated on 26 Nov 2021

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