google-map-react / google-map-react

Google map library for react that allows rendering components as markers :tada:
http://google-map-react.github.io/google-map-react/map/main/
MIT License
6.37k stars 837 forks source link

Next.js: TypeError: Cannot read properties of undefined (reading 'getChildren') #1110

Closed SmartMan1226 closed 1 year ago

SmartMan1226 commented 2 years ago

The default component throws the error before rendering when using it with Next.js.

Screenshots 🖥

Screenshot from 2022-04-22 02-11-41

Environment:

SmartMan1226 commented 2 years ago

Turned out, that this is a version compatibility issue with React@18.

IMPWNG commented 2 years ago

Got the same issue .. I thing we need to downgrade to React@17 ..

Feridun12 commented 2 years ago

Please fix this issue I am a junior dev and don't have that many options when it comes to adding maps to the website😅. Did anyone manage to solve this issue yet? If so please get in contact with me please.

CrissAlvarezH commented 2 years ago

Try remove StrictMode. On next.config.js for Nextjs

const nextConfig = {
  reactStrictMode: false,
}

module.exports = nextConfig
Spencer-lolley commented 2 years ago

Try remove StrictMode. On next.config.js for Nextjs

const nextConfig = {
  reactStrictMode: false,
}

module.exports = nextConfig

still doesnt work

siawfish commented 2 years ago

Any fixes for this please?

siawfish commented 2 years ago

false

turning reactStrictMode: false fixed it for me. Thanks

ASPeXP commented 2 years ago

Thanks

Try remove StrictMode. On next.config.js for Nextjs

const nextConfig = {
  reactStrictMode: false,
}

module.exports = nextConfig

Thanks, it's work., You saved my day.

joaogarin commented 2 years ago

I think the solution mentioned here is not really a great one long term, strictMode can find things that are incompatible with React 18 features in your application, turning it off could have unwanted side effects.

From https://nextjs.org/docs/api-reference/next.config.js/react-strict-mode

Suggested: We strongly suggest you enable Strict Mode in your Next.js application to better prepare your application for the future of React.

skreutzberger commented 2 years ago

Hi, disabling strict mode is not really developer friendly and may leave a Nexjs open to issues. Do you plan to upgrade the library to React 18 and if yes do you already have a potential ETA for it? Thank you 🙏

antoniocosentino commented 2 years ago

I saw that version 2.2.0 was just released (https://github.com/google-map-react/google-map-react/pull/1103) so I upgraded it in my project but I still get the same error. Anybody else experiencing the same issue even after upgrading?

So far I solved it by disabling reactStrictMode as it was suggested here, but I would like to re-enable it as soon as possible

samfromaway commented 2 years ago

I have the same issue Using: "google-map-react": "^2.2.0", "react": "^18.2.0", "next": "^12.2.0",

Error is: TypeError: Cannot read properties of undefined (reading 'getChildren')

If adding reactStrictMode: false it works but obviously this is not a long-term solution.

LucidNinja commented 1 year ago

Same issue. reactStrictMode is just a temporary workaround. Please resolve this.

Hiti3 commented 1 year ago

same here... still nothing?

WashingtonLuiz99 commented 1 year ago

same here... still nothing? help me

financialvice commented 1 year ago

pls fix this is so sad 😫

Reyesnes commented 1 year ago

Hello dears. Suddenly this problem started appearing for me. Every time I'm just writing the code, the terminal window (Output) opens and shows me this error:

2022-09-27 15:27:40 Cannot read properties of undefined (reading 'getChildren0')

Can you help me to solve this please! I am working with SQL language

Cannot read properties

longlostnick commented 1 year ago

I ended up just dumping this library. Turns out it wasn't hard at all just using @googlemaps/react-wrapper and @types/google.maps (for typescript). Just follow these two guides:

Your code will end up looking something like this:

import {Wrapper} from '@googlemaps/react-wrapper';

const App = () => {
  return (
    <Wrapper apiKey={API_KEY}>
      <Map latitude={0} longitude={0} />
    </Wrapper>
  );
};

const Map = ({latitude, longitude}: {latitude: number; longitude: number}) => {
  const ref = React.useRef(null);
  const [map, setMap] = React.useState<google.maps.Map | null>(null);

  React.useEffect(() => {
    if (ref.current && !map) {
      setMap(
        new google.maps.Map(ref.current, {
          zoomControl: true,
          mapTypeControl: false,
          streetViewControl: false,
          center: {
            lat: latitude ?? 0,
            lng: longitude ?? 0,
          },
          zoom: 11,
        })
      );
    }
  }, [ref, map, latitude, longitude]);

  return <div ref={ref} style={{height: '100%', width: '100%'}} />;
};
ckt22 commented 1 year ago

I ended up just dumping this library. Turns out it wasn't hard at all just using @googlemaps/react-wrapper and @types/google.maps (for typescript). Just follow these two guides:

Your code will end up looking something like this:

import {Wrapper} from '@googlemaps/react-wrapper';

const App = () => {
  return (
    <Wrapper apiKey={API_KEY}>
      <Map latitude={0} longitude={0} />
    </Wrapper>
  );
};

const Map = ({latitude, longitude}: {latitude: number; longitude: number}) => {
  const ref = React.useRef(null);
  const [map, setMap] = React.useState<google.maps.Map | null>(null);

  React.useEffect(() => {
    if (ref.current && !map) {
      setMap(
        new google.maps.Map(ref.current, {
          zoomControl: true,
          mapTypeControl: false,
          streetViewControl: false,
          center: {
            lat: latitude ?? 0,
            lng: longitude ?? 0,
          },
          zoom: 11,
        })
      );
    }
  }, [ref, map, latitude, longitude]);

  return <div ref={ref} style={{height: '100%', width: '100%'}} />;
};

Works for me. Thank you very much!

jerachil commented 1 year ago

Finally works for me (Next 13.4.8) by simply adding "use client" the top of the file. Not a really optimal solution though as you have to feed a public google API Key to the client side.

azarshab commented 1 year ago

Try remove StrictMode. On next.config.js for Nextjs

const nextConfig = {
  reactStrictMode: false,
}

module.exports = nextConfig

It worked for me. thank you

volfcan commented 7 months ago

doing strictmode to false is a terrible idea as it helps you to detect unsafe lifecycles, legacy API usage, and other issues. it's like quick workaround but you'll eventually turn it on in the future.

antoniocosentino commented 7 months ago

@volfcan this is not necessary anymore. If you upgrade the library to 2.2.1 everything should work correctly, even with reactStrictMode enabled (at least, that's the case in my projects)

TsuriH commented 1 month ago

@volfcan this is not necessary anymore. If you upgrade the library to 2.2.1 everything should work correctly, even with reactStrictMode enabled (at least, that's the case in my projects)

Upgrade what? Which library to upgrade, unfortunately I'm facing the same issue here

antoniocosentino commented 1 month ago

@TsuriH obviously talking about the google-map-react library, since we are in the issues section of that library