@custom-react-hooks/use-element-size

中文文档
1.5.1发布于 2024/08/18MITTypeScriptCommonJS零依赖近 30 天下载 1.9万

useElementSize is a React hook that enables dynamic tracking of an HTML element's dimensions. It updates the element's width and height in response to window resizing, element mounting/unmounting, and ref changes.

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Installing Only Current Hooks
  4. Installing All Hooks
  5. Importing the Hook
  6. Usage
  7. API Reference
  8. Parameters
  9. Returns object
  10. Use cases
  11. Contributing

useElementSize Hook

useElementSize is a React hook that enables dynamic tracking of an HTML element's dimensions. It updates the element's width and height in response to window resizing, element mounting/unmounting, and ref changes.

Features

  • Dynamic Dimension Tracking: Automatically tracks and updates the width and height of the specified element.
  • Responsive to Environmental Changes: Responds to window resizing and ref changes, ensuring accurate size measurements.
  • SSR Safe: Compatible with server-side rendering, avoiding errors in environments without a window object.
  • Optimized for Accuracy: Uses useLayoutEffect for precise dimension measurements after DOM mutations.

Installation

Choose and install individual hooks that suit your project needs, or install the entire collection for a full suite of utilities.

Installing Only Current Hooks

npm install @custom-react-hooks/use-element-size

or

yarn add @custom-react-hooks/use-element-size

Installing All Hooks

npm install @custom-react-hooks/all

or

yarn add @custom-react-hooks/all

Importing the Hook

The useElementSize hook must be imported using a named import as shown below:

Named Import:

import { useElementSize } from '@custom-react-hooks/use-element-size';

This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.

Usage

import React, { useRef, useState } from 'react';
import { useElementSize } from '@custom-react-hooks/all';

const ElementSizeComponent = () => {
  const [setRef, size] = useElementSize();

  return (
    <div className="element-size">
      <div
        ref={setRef}
        className="resize"
      >
        <p>Drag from the bottom-right corner to resize.</p>
      </div>
      <br />
      <div>Current Size:</div>
      <div className="btns">
        <p>
          Width: <span>{size.width}px</span>
        </p>
        <p>
          Height: <span>{size.height}px</span>
        </p>
      </div>
    </div>
  );
};

export default ElementSizeComponent;

In this example, useElementSize is used to measure and display the dimensions of a div element.

API Reference

Parameters

  • ref: A React ref object attached to the element whose size you want to measure.

Returns object

  • width: width of the element
  • height: height of the element.

Use cases

  • Responsive Components: Adjust component behavior or style based on its size.
  • Layout Calculations: Calculate layout for items like grids or masonry layouts that depend on element sizes.
  • Size-dependent Rendering: Render different content or components based on available size.
  • Animations and Transitions: Trigger animations or transitions when an element's size changes.
  • Optimizing Canvas or SVG: Adjust dimensions for canvas or SVG elements based on their container size.

Contributing

Your contributions to improve useElementSize are welcome. Feel free to submit issues or pull requests to the repository.

版本与发布记录

@custom-react-hooks/use-element-size 共发布 7 个版本,首次发布于 2023/12/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.1latest2024/08/182 年前
1.5.02024/08/182 年前
1.4.192023/12/092 年前
1.4.182023/12/082 年前
1.4.162023/12/072 年前
1.4.152023/12/022 年前

关于 @custom-react-hooks/use-element-size 的常见问题

@custom-react-hooks/use-element-size 是什么?

useElementSize is a React hook that enables dynamic tracking of an HTML element's dimensions. It updates the element's width and height in response to window resizing, element mounting/unmounting, and ref changes.

如何安装 @custom-react-hooks/use-element-size?

在项目目录运行 npm install @custom-react-hooks/use-element-size,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@custom-react-hooks/use-element-size 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@custom-react-hooks/use-element-size 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@custom-react-hooks/use-element-size 可以商用吗?

@custom-react-hooks/use-element-size 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@custom-react-hooks/use-element-size 的下载量有多少?

根据 npm 官方下载统计,@custom-react-hooks/use-element-size 在 2026/10/11 – 2026/11/09 这 30 天内被下载 18,569 次,日均约 619 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@custom-react-hooks/use-element-size 还在维护吗?

最新版本 1.5.1 发布于 2024/08/18(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。