I'm relatively new to react-leaflet and react and am having trouble drawing new components on my map. I've been stuck for a week now so hoping someone can help. I'm attempting to clear the components (layers?) from a Feature Group on the map then render new ones by changing the state of a parent component.
In my minimal example program located at this sandbox I have a few components:
Map
parent component which contains states of map, markers, query data, map bounds, and select box value
// state for initial map loading
let [map, setMap] = useState(null);
// states in Map component
let [data, setData] = useState();
let [bounds, setBounds] = useState(null);
let [featureNum, setFeatureNum] = useState(1);
// some random markers simulating user placed markers on map
let [markers, setMarkers] = useState([
[27.147144835991796, 31.530761718750004],
[28.844673680771795, 33.662109375],
[26.86328062676624, 35.804443359375]
]);
Select
which is a dropdown menu that sets the state of featureNum
when the select box value changes.
Line
which is a Geojson feature that receives the data from data
state and sets the state of markers
. Exists in featuregroup.
Markers
which creates Marker features using the state of markers
. Exists in featuregroup.
After the initial rendering of the map, when the state of featureNum
changes geojson data is gotten asynchronously via the fetch
API in my Map
component. The bounds of the newly received data are calculated on resolve and the state of data
and bounds
are set using the state setters. The featuregroup is cleared using a useRef hook and .clearLayers()
However, no new Line
component appears on the map when data
is set. The map flies to the new bounds where the component should appear. I tried setting the z-index of the featuregroup to be 9999 but that didn't work either.
On selecting a feature from the Select
component, how do I clear the map of the featuregroup and show the new Line
component on the map? Thanks!