The GeoJson component has a function onEachFeature that calls a handler, in this case it is createPopup.
<GeoJSON data={covidGeoJSON} onEachFeature={createPopup}></GeoJSON>
Within the handler createPopup how can I access states defined inside the CovidMap component?
import React, { useState } from 'react'
import { MapContainer, GeoJSON } from 'react-leaflet'
import 'leaflet/dist/leaflet.css'
CovidMap = ({ covidGeoJSON, styles }) => {
const showCords = (e) => {
console.log(e.latlng);
}
const createPopup = (state, layer) => {
layer.on({
mouseover: showCords,
});
layer.bindPopup(state.properties.NAME);
}
const [range, setRange] = useState([]);
return (
<div>
<MapContainer style={{ height: '90vh', width: '100vw' }} center={[39.162497380360634, -94.83672007881789]} zoom={5}>
<GeoJSON data={covidGeoJSON} onEachFeature={createPopup}></GeoJSON>
</MapContainer>
</div>
)
}
export default CovidMap
range
state variable in thecreatePopup
function, you can just directly access it by name. You can pass down the definition of thecreatePopup
handler to your<GeoJSON />
component, but it is invoked in the scope of where it was defined.