Skip to content

Instantly share code, notes, and snippets.

@martinwairegi
Created Dec 14, 2021
Embed
What would you like to do?
function MapWrapper(props) {
// state, ref, and initialization logic eliminated for brevity
// update map if features prop changes - logic formerly put into componentDidUpdate
useEffect( () => {
if (props.features.length) { // may be empty on first render
// set features to map
featuresLayer.setSource(
new VectorSource({
features: props.features // make sure features is an array
})
)
// fit map to feature extent (with 100px of padding)
map.getView().fit(featuresLayer.getSource().getExtent(), {
padding: [100,100,100,100]
})
}
},[props.features])
// return/render logic eliminated for brevity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment