I want to first initialize the location on a map(using latitude and longitude)
And it works as well
I can change Zoom + or - if I put default argument right on useState hook
const [viewport, setViewport] = useState({
width: "100vw",
height: "100vh",
latitude: 47.608013,
longitude: -122.335167,
zoom: 8,
});
return (
<ReactMapGL
{...viewport}
mapStyle="mapbox://styles/thecjreynolds/ck117fnjy0ff61cnsclwimyay"
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_ACCESS_TOKEN}
onViewportChange={setViewport}
onDblClick={showAddMarkerPopUp}
>
// ... more code here
</ReactMapGL>
)
But when I put setViewport in another function(function initMap()).
onViewportChange={initMap} // changed here
The console didn't show any errors and warnings,
But I couldn't change the size of the map or move the map when click it.
const [viewport, setViewport] = useState();
function initMap() {
setViewport({
width: "100vw",
height: "100vh",
latitude: 47.608013,
longitude: -122.335167,
zoom: 8,
});
};
return (
<ReactMapGL
{...viewport}
mapStyle="mapbox://styles/thecjreynolds/ck117fnjy0ff61cnsclwimyay"
mapboxApiAccessToken={process.env.REACT_APP_MAPBOX_ACCESS_TOKEN}
onViewportChange={initMap}
onDblClick={showAddMarkerPopUp}
>
// ... more code here
</ReactMapGL>
)
Plz, tell me why. Thanks.