wrap updateBounds into useCallback

avoids call on every rerender
This commit is contained in:
Winzlieb - 2022-03-11 11:24:19 +01:00
parent 28f422162d
commit baed5ba267
1 changed files with 11 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import React, {useEffect, useState} from 'react'
import React, {useCallback, useEffect, useState} from 'react'
import 'leaflet'
import 'leaflet/dist/leaflet.css'
@ -18,11 +18,16 @@ type LeafletMapProps = {onBoundsChange?: (bounds: L.LatLngBounds) => void}
const BoundsChangeListener = ({onBoundsChange}: {onBoundsChange?: (bounds: L.LatLngBounds) => void}) => {
const map = useMap()
const updateBounds = () => {
onBoundsChange && onBoundsChange(
map.getBounds()
)
}
const updateBounds = useCallback(
() => {
onBoundsChange && onBoundsChange(
map.getBounds()
)
},
[map],
);
useEffect(() => {
updateBounds()
}, [map, updateBounds])