google-maps-react icon indicating copy to clipboard operation
google-maps-react copied to clipboard

Fixing the map position and width

Open 1nfinity-5starZ opened this issue 4 years ago • 1 comments

The way styles are applied are way off, so I came up with a little hack to decouple the ApiWrapper from the UI, here goes the code:

import React, { useEffect, useState } from "react";
import { GoogleApiWrapper, Map } from "google-maps-react";

function ParentComponent(props) {
  const [google, setGoogle] = useState();
  return (
    <div style={{ position: "relative", width: "100%", maxHeight: 500 }}>
      <GoogleProvider onChange={(google) => setGoogle(google)} />
      <Map google={google} />
    </div>
  );
}

function Wrapper(props) {
  useEffect(() => {
    props.onChange(props.google);
  }, [props.google]);

  return null;
}

const GoogleProvider = GoogleApiWrapper({
  apiKey: "",
})(Wrapper);

export default ParentComponent;

The trick is to create a separate component and then pass the google prop up in the hierarchy and store it in the state. Works with flexbox.

1nfinity-5starZ avatar Nov 06 '20 12:11 1nfinity-5starZ

Worked like a charm. Thanks, @1nfinity-5starZ

Shahaed avatar Mar 28 '21 02:03 Shahaed