Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Props not populated #42

Open
aurora10 opened this issue Sep 4, 2022 · 1 comment
Open

React Props not populated #42

aurora10 opened this issue Sep 4, 2022 · 1 comment

Comments

@aurora10
Copy link

aurora10 commented Sep 4, 2022

App.js component that makes Travel Advisor API call that populates the data object:

import React from 'react'
import GoogleMapReact from 'google-map-react'
import {Paper, Typography, useMediaQuery} from '@material-ui/core'
import  LocationOnOutlinedIcon  from '@material-ui/icons/LocationOnOutlined'
import Rating from "@material-ui/lab"

import useStyles from './styles'

const Map = ({setCoordinates, setBounds, coordinates}) => {

  const classes = useStyles()
  const isMobile = useMediaQuery('(min-width: 600px)')
  
  //console.log(coordinates)
  //const coordinates= {lat: 0, lng: 0}
  
  return (
    <div className={classes.mapContainer}>
        <GoogleMapReact
            bootstrapURLKeys={{ key: 'AIzaSyDRtM04BLKwgBtvMIXRD1xRPe1bdHSUDmo'}}
            defaultCenter ={coordinates}
            center = {coordinates}
            defaultZoom = {14}
            margin = {[50, 50, 50, 50]}
            options = {''}
            onChange = {(e) => {
              console.log(e)
              setCoordinates({lat: e.center.lat, lng: e.center.lng});
              
            }}
          
            onChildClick = {''}
        >

        </GoogleMapReact>
    </div>
  )
}

export default Mapconst App = () => {
  const [places, setPlaces] = useState([]);
  const [coordinates, setCoordinates] = useState({});
  const [bounds, setBounds] = useState(null);

  useEffect(() => {
    getPlacesData().then((data) => {
    console.log(data) // data is there
      setPlaces(data);
    });
  }, []);

  return (
    <>
      <CssBaseline />
      <Header />
      <Grid container spacing={3} style={{ width: "100%" }}>
        <Grid item xs={12} md={4}>
          <List />
        </Grid>

        <Grid item xs={12} md={8}>
          <Map
            setCoordinates={setCoordinates}
            setBounds={setBounds}
            coordinates={coordinates}
          />
        </Grid>
      </Grid>
    </>
  );
};

export default App;

For some reason coordinates prop is not populated in onChange as seen in the video. I double check the code and cannot find what is stopping it from getting the data:

import React from 'react'
import GoogleMapReact from 'google-map-react'
import {Paper, Typography, useMediaQuery} from '@material-ui/core'
import  LocationOnOutlinedIcon  from '@material-ui/icons/LocationOnOutlined'
import Rating from "@material-ui/lab"

import useStyles from './styles'

const Map = ({setCoordinates, setBounds, coordinates}) => {

  const classes = useStyles()
  const isMobile = useMediaQuery('(min-width: 600px)')
  
  //console.log(coordinates)
  //const coordinates= {lat: 0, lng: 0}
  
  return (
    <div className={classes.mapContainer}>
        <GoogleMapReact
            bootstrapURLKeys={{ key: 'AIzaSyDRtM04BLKwgBtvMIXRD1xRPe1bdHSUDmo'}}
            defaultCenter ={coordinates}
            center = {coordinates}
            defaultZoom = {14}
            margin = {[50, 50, 50, 50]}
            options = {''}
            onChange = {(e) => {
              console.log(e) // this is empty
              setCoordinates({lat: e.center.lat, lng: e.center.lng});
              
            }}
          
            onChildClick = {''}
        >

        </GoogleMapReact>
    </div>
  )
}

export default Map


The API call returns a bunch of restaurants, so the call works. But I cannot get {coordinates} prop populated....

@shivendra8004
Copy link

Hey bro @aurora10 i can feel your pain i also got stuck on same place, to resolve this error do what i said below

  1. const [coordinates, setCoordinates] = useState({}); // Here add coordinates of any place
    for eg.
    const [coordinates, setCoordinates] = useState({ lat: 0, lng: 0 });

  2. const [bounds, setBounds] = useState(null); // Instead of null replace it by {}
    for eg.
    const [bounds, setBounds] = useState({});

If your error get resolved than mark this issue as resolved.
@adrianhajdin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants