用于判断组件大小的React钩子
用于判断组件大小的React钩子
React 其它杂项
共209Star
详细介绍
@rehooks/component-size
React hook for determining the size of a component
Note: This is using the new React Hooks API Proposal which is subject to change until React 16.7 final.
You'll need to install
react
,react-dom
, etc at^16.7.0-alpha.0
Install
yarn add @rehooks/component-size
Usage
import { useRef } from 'react'
import useComponentSize from '@rehooks/component-size'
function MyComponent() {
let ref = useRef(null)
let size = useComponentSize(ref)
// size == { width: 100, height: 200 }
let { width, height } = size
let imgUrl = `https://via.placeholder.com/${width}x${height}`
return (
<div style={{ width: '100%', height: '100%' }}>
<img ref={ref} src={imgUrl} />
</div>
)
}
-
124 Star
-
192 Star
-
3591 Star
-
3589 Star
-
1497 Star
-
636 Star
-
260 Star
-
1118 Star
-
353 Star