I need to set new lat and lng to my google map in React
Const mapRef return error: Object is possibly 'null'. TS2531
When i used let instead of the React.useRef it works.
I think a should set type to mapRef, but i dont know which one and where i can find it.
But I think useRef is better solution, isnt it?
Google maps library: https://www.npmjs.com/package/@react-google-maps/api
const libraries = ["places"];
const CustomMap = () => {
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: "MY_API_KEY",
libraries,
});
const options = {
disableDefaultUI: true,
zoomControl: true,
};
const mapRef = React.useRef();
const onMapLoad = React.useCallback((map) => {
mapRef.current = map;
}, []);
const panTo = React.useCallback(({ lat, lng }) => {
if (null !== mapRef.current) {
// There is error
// mapRef.current Object is possibly 'null'. TS2531
mapRef.current.panTo({ lat, lng });
mapRef.current.setZoom(18);
}
}, []);
//console.log("maps show coord: ", props.coordinates);
if (loadError) {
return (
<Fragment>
<p>Error</p>
</Fragment>
);
}
if (!isLoaded) {
return (
<Fragment>
<p>loading</p>
</Fragment>
);
}
return (
<div>
<Search panTo={panTo} />
<GoogleMap
id="map"
mapContainerStyle={mapContainerStyle}
zoom={15}
center={center}
//options={options}
onLoad={onMapLoad}
/>
</div>
);
};
GoogleMap
component? – Rodrigo Ehlers