Skip to content

Commit

Permalink
Only update state if the width or height change
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio-Mira <[email protected]>
  • Loading branch information
Sergio-Mira committed Feb 5, 2023
1 parent 36e5057 commit 5cdbd01
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
var React = require('react')
var useState = React.useState
var useCallback = React.useCallback
var useLayoutEffect = React.useLayoutEffect
const React = require('react')
const useState = React.useState
const useCallback = React.useCallback
const useLayoutEffect = React.useLayoutEffect

function getSize(el) {
if (!el) {
Expand All @@ -19,14 +19,18 @@ function getSize(el) {
}

function useComponentSize(ref) {
var _useState = useState(getSize(ref ? ref.current : {}))
var ComponentSize = _useState[0]
var setComponentSize = _useState[1]
const _useState = useState(getSize(ref ? ref.current : {}))
const ComponentSize = _useState[0]
const setComponentSize = _useState[1]

var handleResize = useCallback(
const handleResize = useCallback(
function handleResize() {
if (ref.current) {
setComponentSize(getSize(ref.current))
const newSize = getSize(ref.current)

if (ComponentSize.width !== newSize.width && ComponentSize.height !== newSize.height) {
setComponentSize(newSize)
}
}
},
[ref]
Expand Down

0 comments on commit 5cdbd01

Please sign in to comment.