Skip to content

Commit

Permalink
fix: no rect render on scroll, more frequent axis measuring
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Oct 1, 2020
1 parent 14f06c5 commit ce78bac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/components/AxisLinear.useMeasure.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import useChartState from '../hooks/useChartState'
import useIsomorphicLayoutEffect from '../hooks/useIsomorphicLayoutEffect'
import { round } from '../utils/Utils'

const getElBox = el => {
var rect = el.getBoundingClientRect()
Expand Down Expand Up @@ -218,7 +217,7 @@ export default function useMeasure({
])

// Measure after if needed
React.useEffect(() => {
useIsomorphicLayoutEffect(() => {
measureDimensions()
}, [measureDimensions])
})
}
13 changes: 11 additions & 2 deletions src/hooks/useHyperResponsive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect'

export default function useRect(nodeRef) {
const [element, setElement] = React.useState(nodeRef.current?.parentElement)
const [rect, setRect] = React.useState({ width: 0, height: 0 })
const [rect, _setRect] = React.useState({ width: 0, height: 0 })
const initialRectSet = React.useRef(false)

const setRect = React.useCallback(value => {
_setRect(old => {
if (old.width !== value.width || old.height !== value.height) {
return value
}
return old
})
}, [])

useIsomorphicLayoutEffect(() => {
if (nodeRef.current?.parentElement !== element) {
setElement(nodeRef.current?.parentElement)
Expand All @@ -34,7 +43,7 @@ export default function useRect(nodeRef) {
return () => {
observer.unobserve()
}
}, [element])
}, [element, setRect])

return { width: rect.width, height: rect.height }
}

0 comments on commit ce78bac

Please sign in to comment.