I am using the GoogleMapReact component and need the coords and setCoords to work properly. I am receiving this error message whenever I try to start my application.
Can anyone explain to me what I did wrong?
I declared coords and setCoords in my app.js to have an empty value in the UseState and I think thats where my issue is coming from but if anyone can chime in I would appreciate it.
This is my app.js
const App = () => {
const [type, setType] = useState('restaurants');
const [rating, setRating] = useState('');
const [coords, setCoords] = useState();
const [bounds, setBounds] = useState(null);
const [weatherData, setWeatherData] = useState([]);
const [filteredPlaces, setFilteredPlaces] = useState([]);
const [places, setPlaces] = useState([]);
const [autocomplete, setAutocomplete] = useState(null);
const [childClicked, setChildClicked] = useState(null);
const [isLoading, setIsLoading] = useState(false);
console.log(coords);
useEffect(() => {
navigator.geolocation.getCurrentPosition(({ coords: {latitude, longitude} }) => {
setCoords({ lat: latitude, lng: longitude});
});
}, []);
useEffect(() => {
const filtered = places.filter((place) => Number(place.rating) > rating);
setFilteredPlaces(filtered);
}, [rating]);
useEffect(() => {
if(bounds) {
setIsLoading(true);
getWeatherData(coords.lat, coords.lng)
.then((data) => setWeatherData(data));
getPlacesData(type, bounds.sw, bounds.ne)
.then((data) => {
setPlaces(data.filter((place) => place.name && place.num_reviews > 0));
setFilteredPlaces([]);
setRating('');
setIsLoading(false);
});
}
},[bounds, type, coords]);
const onLoad = (autoC) => setAutocomplete(autoC);
const onPlaceChanged = () => {
const lat = autocomplete.getPlace().geometry.location.lat();
const lng = autocomplete.getPlace().geometry.location.lng();
setCoords({ lat, lng });
};
return (
<>
<CssBaseline />
<Header onPlaceChanged={onPlaceChanged} onLoad={onLoad} />
<Grid container spacing ={3} style={{ width: '100%' }}>
<Grid item xs={12} md={4}>
<List
places={filteredPlaces.length ? filteredPlaces : places}
childClicked={childClicked}
isLoading={isLoading}
type={type}
setType={setType}
setRating={setRating}
rating={rating}
/>
</Grid>
<Grid item xs={12} md={8} style={{display: 'flex', justfiyContent: 'center', alignItems: 'center' }}>
{coords && <Map
setChildClicked={setChildClicked}
setCoordinates={setCoords}
setBounds={setBounds}
coords={coords}
places={filteredPlaces?.length ? filteredPlaces : places}
weatherData={weatherData}
/>}
</Grid>
</Grid>
</>
);
};
export default App;
this is my map.jsx
const Map = ({coords, setCoords, setBounds, places, setChildClicked, weatherData}) => {
const classes = useStyles();
const matches = useMediaQuery('(min-width:600px)');
return (
<div
className={classes.mapContainer}>
<GoogleMapReact
bootstrapURLKeys={{ key: process.env.REACT_APP_GOOGLE_MAPS_API_KEY }}
defaultCenter={coords}
center={coords}
defaultZoom={14}
margin={[50, 50, 50, 50,]}
options={{ disableDefaultUI: true, zoomControl: true, styles: mapStyles }}
onChange={(e) => {
setCoords({ lat: e.center.lat, lng: e.center.lng });
setBounds({ ne: e.marginBounds.ne, sw: e.marginBounds.sw });
}}
onChildClick={(child) => setChildClicked(child)}
>
{places?.length && places?.map((place, i) => (
<div
className={classes.markerContainer}
lat={Number(place.latitude)}
lng={Number(place.longitude)}
key={i}
>
{!matches ?
<LocationOnOutlinedIcon color="primary" fontSize="large" />
: (
<Paper elevation={3} className={classes.paper}>
<Typography className={classes.typography} variant="subtitle2" gutterBottom> {place.name}</Typography>
<img
className={classes.pointer}
src={place.photo ? place.photo.images.large.url : 'https://www.rlare.com/wp-content/uploads/2019/12/Inside-1-1.jpg'}
alt={place.name}
/>
<Rating size="small" value={Number(place.rating)} name="read-only" />
</Paper>
)}
</div>
))}
{weatherData?.list?.length && weatherData.list.map((data, i) => (
<div key={i} lat={data.coord.lat} lng={data.coord.lon}>
<img src={`https://openweathermap.org/img/w/${data.weather[0].icon}.png`} height="70px" alt="map"/>
</div>
))}
</GoogleMapReact>
</div>
);
}
export default Map;