Open FleetAdmiralJakob opened 1 year ago
use useRef hook.
//Code const mapRef = useRef(null);
useEffect(() => { if (mapRef.current && lat !== undefined && lng !== undefined) { mapRef.current.setView([lat, lng], mapRef.current.getZoom()); } }, [lat, lng]);
<MapContainer center={[lat, lng]} zoom={13} ref={mapRef}
{...your code}
//Code
Explanation:
How can I make that if I change a state that stores the coordinates in a higher component and pass the state as a prop to the map component. How can I make that the center of the map changes if the state changes?