Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update state if the width or height change #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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