I tried watching videos and articles and copied almost the same code but this error doesn't go useRef() always shows up
import React from "react";
import { GoogleMap, useJsApiLoader } from "@react-google-maps/api";
const containerStyle = {
width: "400px",
height: "400px",
};
const center = {
lat: -3.745,
lng: -38.523,
};
function MapComponent() {
const { isLoaded } = useJsApiLoader({
id: "google-map-script",
googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY,
});
const [map, setMap] = React.useState(null);
const onLoad = React.useCallback(function callback(map) {
const bounds = new window.google.maps.LatLngBounds(center);
map.fitBounds(bounds);
setMap(map);
}, []);
const onUnmount = React.useCallback(function callback(map) {
setMap(null);
}, []);
return isLoaded ? (
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
onLoad={onLoad}
onUnmount={onUnmount}
>
{/* Child components, such as markers, info windows, etc. */}
<></>
</GoogleMap>
) : (
<></>
);
}
export default React.memo(MapComponent);
This is how I implement the component
<div style={{ height: "100vh", width: "100%" }}>
<MapComponent />
</div>
Error
ERROR
Cannot read properties of null (reading 'useRef')
TypeError: Cannot read properties of null (reading 'useRef')
at useRef (http://localhost:3000/static/js/bundle.js:129416:25)
at useJsApiLoader (http://localhost:3000/static/js/bundle.js:86513:64)
at MapComponent (http://localhost:3000/static/js/bundle.js:3971:77)
at renderWithHooks (http://localhost:3000/static/js/bundle.js:55933:22)
at updateFunctionComponent (http://localhost:3000/static/js/bundle.js:58813:24)
at updateSimpleMemoComponent (http://localhost:3000/static/js/bundle.js:58675:14)
at updateMemoComponent (http://localhost:3000/static/js/bundle.js:58548:18)
at beginWork (http://localhost:3000/static/js/bundle.js:60578:20)
at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:45529:18)
at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:45573:20)
Top comments (1)
This is the console error.