Skip to content

Commit

Permalink
perf: avoid force reflow in use-size (#2068)
Browse files Browse the repository at this point in the history
* fix: avoid force reflow in use-size

* fix: update size init logic

* docs: there is no need comment for simple logic

---------

Co-authored-by: lijianan <[email protected]>
Co-authored-by: 云泥 <[email protected]>
  • Loading branch information
3 people authored Feb 28, 2023
1 parent 29c9203 commit 1da40fe
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/hooks/src/useSize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import useIsomorphicLayoutEffectWithTarget from '../utils/useIsomorphicLayoutEff
type Size = { width: number; height: number };

function useSize(target: BasicTarget): Size | undefined {
let el = getTargetElement(target);
const [state, setState] = useRafState<Size | undefined>(
el ? { width: el.clientWidth, height: el.clientHeight } : undefined,
() => {
const el = getTargetElement(target);
return el ? { width: el.clientWidth, height: el.clientHeight } : undefined
},
);

useIsomorphicLayoutEffectWithTarget(
() => {
el = getTargetElement(target);
const el = getTargetElement(target);

if (!el) {
return;
Expand Down

0 comments on commit 1da40fe

Please sign in to comment.