Getting Two react components to speak to each other with 'google-maps-react'











up vote
0
down vote

favorite












So I am still new to React, I have two components, one creates a map and the other lists places on the map, I am trying to find a way so that when one of the list items is clicked that the InfoWindow on the map pops up.



This is the Map Component



import React, { Component } from 'react';
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},

{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
}
]

class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
};

onMarkerClick = (props, marker, e) =>
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});

onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
};

render() {
return (
<div className = 'map-container' style={{marginleft:'250px'}}>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
<Marker
onClick={this.onMarkerClick}
title = {AllPlaces[0].name}
name={AllPlaces[0].name}
position = {{lat:AllPlaces[0].lat,lng:AllPlaces[0].lng}}/>
<Marker
onClick={this.onMarkerClick}
title = {AllPlaces[1].name}
name={AllPlaces[1].name}
position = {{lat:AllPlaces[1].lat,lng:AllPlaces[1].lng}}/>
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)


And this is the List Component



{import React, { Component } from 'react';
import { GoogleApiWrapper, InfoWindow, Map, Marker } from 'google-maps-
react';

var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},

{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
}
]

class ListPlaces extends Component {

CreateInputField = () => {
return <input
placeholder = "Search Nearby Places"
/>
}

findPlaces = () => {
return(
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
<li
key = {index}
className='Place'
>{arrayItem.name}</li>
)}
</ol>
)
}

render() {
return(
<div>
<div className = 'sideMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat </h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
{this.findPlaces()}
</div>
</div>
</div>
)
}
}
export default ListPlaces
}


I have no idea how I can make it so that when one of the items in the list component are clicked that the correct InfoWindow will show.










share|improve this question


























    up vote
    0
    down vote

    favorite












    So I am still new to React, I have two components, one creates a map and the other lists places on the map, I am trying to find a way so that when one of the list items is clicked that the InfoWindow on the map pops up.



    This is the Map Component



    import React, { Component } from 'react';
    import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

    var AllPlaces = [
    {
    "name" : "Pizza",
    "lat": "40.7589",
    "lng":"-73.9851",
    },

    {
    "name" : "Cookies",
    "lat": "40.7690",
    "lng":"-73.9952",
    }
    ]

    class MapContainer extends Component {
    state = {
    showingInfoWindow: false,
    activeMarker: {},
    selectedPlace: {},
    };

    onMarkerClick = (props, marker, e) =>
    this.setState({
    selectedPlace: props,
    activeMarker: marker,
    showingInfoWindow: true
    });

    onMapClicked = (props) => {
    if (this.state.showingInfoWindow) {
    this.setState({
    showingInfoWindow: false,
    activeMarker: null
    })
    }
    };

    render() {
    return (
    <div className = 'map-container' style={{marginleft:'250px'}}>
    <Map google={this.props.google} zoom={14}
    initialCenter = {{lat:40.7589, lng:-73.9851}}
    onClick={this.onMapClicked}>
    <Marker
    onClick={this.onMarkerClick}
    title = {AllPlaces[0].name}
    name={AllPlaces[0].name}
    position = {{lat:AllPlaces[0].lat,lng:AllPlaces[0].lng}}/>
    <Marker
    onClick={this.onMarkerClick}
    title = {AllPlaces[1].name}
    name={AllPlaces[1].name}
    position = {{lat:AllPlaces[1].lat,lng:AllPlaces[1].lng}}/>
    <InfoWindow
    onOpen={this.windowHasOpened}
    onClose={this.windowHasClosed}
    marker={this.state.activeMarker}
    visible={this.state.showingInfoWindow}>
    <div>
    <h1>{this.state.selectedPlace.name}</h1>
    </div>
    </InfoWindow>
    </Map>
    </div>
    );
    }
    }

    export default GoogleApiWrapper({
    apiKey: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
    })(MapContainer)


    And this is the List Component



    {import React, { Component } from 'react';
    import { GoogleApiWrapper, InfoWindow, Map, Marker } from 'google-maps-
    react';

    var AllPlaces = [
    {
    "name" : "Pizza",
    "lat": "40.7589",
    "lng":"-73.9851",
    },

    {
    "name" : "Cookies",
    "lat": "40.7690",
    "lng":"-73.9952",
    }
    ]

    class ListPlaces extends Component {

    CreateInputField = () => {
    return <input
    placeholder = "Search Nearby Places"
    />
    }

    findPlaces = () => {
    return(
    <ol className='Places'>
    {AllPlaces.map((arrayItem, index)=>
    <li
    key = {index}
    className='Place'
    >{arrayItem.name}</li>
    )}
    </ol>
    )
    }

    render() {
    return(
    <div>
    <div className = 'sideMenu'>
    <div className = 'List'>
    <h1 className = 'title'> Places to Eat </h1>
    {this.CreateInputField()}
    </div>
    <div className = 'PlaceList'>
    {this.findPlaces()}
    </div>
    </div>
    </div>
    )
    }
    }
    export default ListPlaces
    }


    I have no idea how I can make it so that when one of the items in the list component are clicked that the correct InfoWindow will show.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      So I am still new to React, I have two components, one creates a map and the other lists places on the map, I am trying to find a way so that when one of the list items is clicked that the InfoWindow on the map pops up.



      This is the Map Component



      import React, { Component } from 'react';
      import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

      var AllPlaces = [
      {
      "name" : "Pizza",
      "lat": "40.7589",
      "lng":"-73.9851",
      },

      {
      "name" : "Cookies",
      "lat": "40.7690",
      "lng":"-73.9952",
      }
      ]

      class MapContainer extends Component {
      state = {
      showingInfoWindow: false,
      activeMarker: {},
      selectedPlace: {},
      };

      onMarkerClick = (props, marker, e) =>
      this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true
      });

      onMapClicked = (props) => {
      if (this.state.showingInfoWindow) {
      this.setState({
      showingInfoWindow: false,
      activeMarker: null
      })
      }
      };

      render() {
      return (
      <div className = 'map-container' style={{marginleft:'250px'}}>
      <Map google={this.props.google} zoom={14}
      initialCenter = {{lat:40.7589, lng:-73.9851}}
      onClick={this.onMapClicked}>
      <Marker
      onClick={this.onMarkerClick}
      title = {AllPlaces[0].name}
      name={AllPlaces[0].name}
      position = {{lat:AllPlaces[0].lat,lng:AllPlaces[0].lng}}/>
      <Marker
      onClick={this.onMarkerClick}
      title = {AllPlaces[1].name}
      name={AllPlaces[1].name}
      position = {{lat:AllPlaces[1].lat,lng:AllPlaces[1].lng}}/>
      <InfoWindow
      onOpen={this.windowHasOpened}
      onClose={this.windowHasClosed}
      marker={this.state.activeMarker}
      visible={this.state.showingInfoWindow}>
      <div>
      <h1>{this.state.selectedPlace.name}</h1>
      </div>
      </InfoWindow>
      </Map>
      </div>
      );
      }
      }

      export default GoogleApiWrapper({
      apiKey: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
      })(MapContainer)


      And this is the List Component



      {import React, { Component } from 'react';
      import { GoogleApiWrapper, InfoWindow, Map, Marker } from 'google-maps-
      react';

      var AllPlaces = [
      {
      "name" : "Pizza",
      "lat": "40.7589",
      "lng":"-73.9851",
      },

      {
      "name" : "Cookies",
      "lat": "40.7690",
      "lng":"-73.9952",
      }
      ]

      class ListPlaces extends Component {

      CreateInputField = () => {
      return <input
      placeholder = "Search Nearby Places"
      />
      }

      findPlaces = () => {
      return(
      <ol className='Places'>
      {AllPlaces.map((arrayItem, index)=>
      <li
      key = {index}
      className='Place'
      >{arrayItem.name}</li>
      )}
      </ol>
      )
      }

      render() {
      return(
      <div>
      <div className = 'sideMenu'>
      <div className = 'List'>
      <h1 className = 'title'> Places to Eat </h1>
      {this.CreateInputField()}
      </div>
      <div className = 'PlaceList'>
      {this.findPlaces()}
      </div>
      </div>
      </div>
      )
      }
      }
      export default ListPlaces
      }


      I have no idea how I can make it so that when one of the items in the list component are clicked that the correct InfoWindow will show.










      share|improve this question













      So I am still new to React, I have two components, one creates a map and the other lists places on the map, I am trying to find a way so that when one of the list items is clicked that the InfoWindow on the map pops up.



      This is the Map Component



      import React, { Component } from 'react';
      import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

      var AllPlaces = [
      {
      "name" : "Pizza",
      "lat": "40.7589",
      "lng":"-73.9851",
      },

      {
      "name" : "Cookies",
      "lat": "40.7690",
      "lng":"-73.9952",
      }
      ]

      class MapContainer extends Component {
      state = {
      showingInfoWindow: false,
      activeMarker: {},
      selectedPlace: {},
      };

      onMarkerClick = (props, marker, e) =>
      this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true
      });

      onMapClicked = (props) => {
      if (this.state.showingInfoWindow) {
      this.setState({
      showingInfoWindow: false,
      activeMarker: null
      })
      }
      };

      render() {
      return (
      <div className = 'map-container' style={{marginleft:'250px'}}>
      <Map google={this.props.google} zoom={14}
      initialCenter = {{lat:40.7589, lng:-73.9851}}
      onClick={this.onMapClicked}>
      <Marker
      onClick={this.onMarkerClick}
      title = {AllPlaces[0].name}
      name={AllPlaces[0].name}
      position = {{lat:AllPlaces[0].lat,lng:AllPlaces[0].lng}}/>
      <Marker
      onClick={this.onMarkerClick}
      title = {AllPlaces[1].name}
      name={AllPlaces[1].name}
      position = {{lat:AllPlaces[1].lat,lng:AllPlaces[1].lng}}/>
      <InfoWindow
      onOpen={this.windowHasOpened}
      onClose={this.windowHasClosed}
      marker={this.state.activeMarker}
      visible={this.state.showingInfoWindow}>
      <div>
      <h1>{this.state.selectedPlace.name}</h1>
      </div>
      </InfoWindow>
      </Map>
      </div>
      );
      }
      }

      export default GoogleApiWrapper({
      apiKey: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
      })(MapContainer)


      And this is the List Component



      {import React, { Component } from 'react';
      import { GoogleApiWrapper, InfoWindow, Map, Marker } from 'google-maps-
      react';

      var AllPlaces = [
      {
      "name" : "Pizza",
      "lat": "40.7589",
      "lng":"-73.9851",
      },

      {
      "name" : "Cookies",
      "lat": "40.7690",
      "lng":"-73.9952",
      }
      ]

      class ListPlaces extends Component {

      CreateInputField = () => {
      return <input
      placeholder = "Search Nearby Places"
      />
      }

      findPlaces = () => {
      return(
      <ol className='Places'>
      {AllPlaces.map((arrayItem, index)=>
      <li
      key = {index}
      className='Place'
      >{arrayItem.name}</li>
      )}
      </ol>
      )
      }

      render() {
      return(
      <div>
      <div className = 'sideMenu'>
      <div className = 'List'>
      <h1 className = 'title'> Places to Eat </h1>
      {this.CreateInputField()}
      </div>
      <div className = 'PlaceList'>
      {this.findPlaces()}
      </div>
      </div>
      </div>
      )
      }
      }
      export default ListPlaces
      }


      I have no idea how I can make it so that when one of the items in the list component are clicked that the correct InfoWindow will show.







      javascript reactjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 22:55









      Hyunjin Kim

      173




      173
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can define a method within the parent component which retrieves the clicked item information and stores them in its state. Then give the value of the state to the other component. Something like:



          // Parent.js

          class Parent extends Component {
          state = {
          ...
          position: null;
          }

          ...

          setPosition = (position) => {
          this.setState({ position })
          }

          render () {
          return (
          <Fragment>
          <A position={this.state.position} />
          <B setPosition={this.setPosition} />
          </Fragment>
          )
          }
          }

          // B.js

          class B extends Component {
          ...

          notifyParent = (item) => {
          this.props.setPosition(item)
          }

          render () {
          return (
          <ul>
          {items && items.map(item => (
          <li onClick={() => this.notifyParent(item)}>
          {item.key}
          </li>
          ))}
          </ul>
          )
          }
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290743%2fgetting-two-react-components-to-speak-to-each-other-with-google-maps-react%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You can define a method within the parent component which retrieves the clicked item information and stores them in its state. Then give the value of the state to the other component. Something like:



            // Parent.js

            class Parent extends Component {
            state = {
            ...
            position: null;
            }

            ...

            setPosition = (position) => {
            this.setState({ position })
            }

            render () {
            return (
            <Fragment>
            <A position={this.state.position} />
            <B setPosition={this.setPosition} />
            </Fragment>
            )
            }
            }

            // B.js

            class B extends Component {
            ...

            notifyParent = (item) => {
            this.props.setPosition(item)
            }

            render () {
            return (
            <ul>
            {items && items.map(item => (
            <li onClick={() => this.notifyParent(item)}>
            {item.key}
            </li>
            ))}
            </ul>
            )
            }
            }





            share|improve this answer

























              up vote
              0
              down vote













              You can define a method within the parent component which retrieves the clicked item information and stores them in its state. Then give the value of the state to the other component. Something like:



              // Parent.js

              class Parent extends Component {
              state = {
              ...
              position: null;
              }

              ...

              setPosition = (position) => {
              this.setState({ position })
              }

              render () {
              return (
              <Fragment>
              <A position={this.state.position} />
              <B setPosition={this.setPosition} />
              </Fragment>
              )
              }
              }

              // B.js

              class B extends Component {
              ...

              notifyParent = (item) => {
              this.props.setPosition(item)
              }

              render () {
              return (
              <ul>
              {items && items.map(item => (
              <li onClick={() => this.notifyParent(item)}>
              {item.key}
              </li>
              ))}
              </ul>
              )
              }
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can define a method within the parent component which retrieves the clicked item information and stores them in its state. Then give the value of the state to the other component. Something like:



                // Parent.js

                class Parent extends Component {
                state = {
                ...
                position: null;
                }

                ...

                setPosition = (position) => {
                this.setState({ position })
                }

                render () {
                return (
                <Fragment>
                <A position={this.state.position} />
                <B setPosition={this.setPosition} />
                </Fragment>
                )
                }
                }

                // B.js

                class B extends Component {
                ...

                notifyParent = (item) => {
                this.props.setPosition(item)
                }

                render () {
                return (
                <ul>
                {items && items.map(item => (
                <li onClick={() => this.notifyParent(item)}>
                {item.key}
                </li>
                ))}
                </ul>
                )
                }
                }





                share|improve this answer












                You can define a method within the parent component which retrieves the clicked item information and stores them in its state. Then give the value of the state to the other component. Something like:



                // Parent.js

                class Parent extends Component {
                state = {
                ...
                position: null;
                }

                ...

                setPosition = (position) => {
                this.setState({ position })
                }

                render () {
                return (
                <Fragment>
                <A position={this.state.position} />
                <B setPosition={this.setPosition} />
                </Fragment>
                )
                }
                }

                // B.js

                class B extends Component {
                ...

                notifyParent = (item) => {
                this.props.setPosition(item)
                }

                render () {
                return (
                <ul>
                {items && items.map(item => (
                <li onClick={() => this.notifyParent(item)}>
                {item.key}
                </li>
                ))}
                </ul>
                )
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 at 1:52









                Masious

                354213




                354213






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290743%2fgetting-two-react-components-to-speak-to-each-other-with-google-maps-react%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?