react-native-maps: this.getNativeComponent is not a function (it is undefined)

Summary

I am trying to use <Polyline /> component, however the app crashes with the this.getNativeComponent is not a function (it is undefined) Currently crashing on android, I haven’t tried on iOS.

Screenshot_20231009-095255_pow-app-2

Reproducible sample code

import MapView, { PROVIDER_GOOGLE, Polyline } from 'react-native-maps';

const Map = () => {
  return (
    <MapView
      provider={PROVIDER_GOOGLE}
      initialRegion={{
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      }}
      style={{ height: '100%', width: '100%' }}
    >
      <Polyline
        coordinates={[{"latitude": 40.44375, "longitude": -79.95392}, {"latitude": 40.44355, "longitude": -79.95368}, {"latitude": 40.44327, "longitude": -79.95355}, {"latitude": 40.44329, "longitude": -79.95345}, {"latitude": 40.44317, "longitude": -79.9534}, {"latitude": 40.44313, "longitude": -79.95336}, {"latitude": 40.44305, "longitude": -79.95316}, {"latitude": 40.44301, "longitude": -79.95313}, {"latitude": 40.44298, "longitude": -79.95307}, {"latitude": 40.44259, "longitude": -79.95261}, {"latitude": 40.44255, "longitude": -79.95265}, {"latitude": 40.44244, "longitude": -79.95262}, {"latitude": 40.4424, "longitude": -79.95254}, {"latitude": 40.44238, "longitude": -79.95248}, {"latitude": 40.44239, "longitude": -79.9524}, {"latitude": 40.4424, "longitude": -79.95239}, {"latitude": 40.44231, "longitude": -79.95227}, {"latitude": 40.4421, "longitude": -79.95249}, {"latitude": 40.442, "longitude": -79.95258}]}
      />
    </MapView>
  )
}

export default Map

Steps to reproduce

  • install the latest expo (typescript template)
  • download the necessary dependencies
  • copy the code above

Expected result

Map renders with polyline

Actual result

App crashes with this.getNativeComponent is not a function (it is undefined)

React Native Maps Version

1.7.1

What platforms are you seeing the problem on?

Android

React Native Version

0.72.5

What version of Expo are you using?

SDK 48

Device(s)

Samsung A32

Additional information

I am using Expo 49, the dropdown doesn’t have that option. If I remove the <Polyline /> component, the map component <MapView /> itself works no issue. I also tried to use <Marker />, the same error pops up.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 13
  • Comments: 46

Most upvoted comments

For anyone landing here recently: just go to node_modules/react-native-maps/lib/MapMarker.js:32 and delete: getNativeComponent;

i removed some babel plugins and removed node_modules then reinstalled and restart if not working with ‘module:metro-react-native-babel-preset’ then use ‘module:@react-native/babel-preset’

npm remove @babel/plugin-transform-class-properties @babel/plugin-transform-private-methods @babel/plugin-transform-private-property-in-object
module.exports = {
  presets: ['module:@react-native/babel-preset'],
  plugins: [
    'react-native-reanimated/plugin',
    [
      'module-resolver',
      {
        alias: {
          '@': './src',
        },
      },
    ],
  ],
};

I changed module:metro-react-native-babel-preset -> module:@react-native/babel-preset, and problem solved!

// babel.config.js
module.exports = {
  presets: ['module:@react-native/babel-preset'],
};

When I use module:metro-react-native-babel-preset, I have to install @babel/plugin-transform-private-methods . And above solutions (overrides) doesn’t work for me. So, I changed preset and don’t install that problematic plugin( @babel/plugin-transform-private-methods ).


This is my source code: https://github.com/S-KEEP/S-KEEP_Front/pull/17/commits/29c580d1826e0fee163ae98bb8ac5073e9d4e9ea

I have the same issue when I added a Marker. My react-native version is 0.74.6, any solution??

by removing the getNativeComponent you can’t render markers anymore, correct?

For anyone landing here recently: just go to node_modules/react-native-maps/lib/MapMarker.js:32 and delete: getNativeComponent;

This worked for me too, but I agree its a hucky solution. Any other solution ?

Meanwhile the patch-package: react-native-maps+1.15.6.patch

diff --git a/node_modules/react-native-maps/lib/MapMarker.js b/node_modules/react-native-maps/lib/MapMarker.js
index 90b689e..0df64b6 100644
--- a/node_modules/react-native-maps/lib/MapMarker.js
+++ b/node_modules/react-native-maps/lib/MapMarker.js
@@ -29,7 +29,6 @@ const react_native_1 = require("react-native");
 const decorateMapComponent_1 = __importStar(require("./decorateMapComponent"));
 const MapMarkerNativeComponent_1 = require("./MapMarkerNativeComponent");
 class MapMarker extends React.Component {
-    getNativeComponent;
     getMapManagerCommand;
     getUIManagerCommand;
     static Animated;

This is what I’m doing for my project so that every time I reinstall dependencies with npm i the marker works.

  1. Created a file called react-native-maps+1.20.1.patch with the following contents:
--- node_modules/react-native-maps/lib/MapMarker.js
+++ node_modules/react-native-maps/lib/MapMarker.js
@@ -29,7 +29,6 @@
 const decorateMapComponent_1 = __importStar(require("./decorateMapComponent"));
 const MapMarkerNativeComponent_1 = require("./MapMarkerNativeComponent");
 class MapMarker extends React.Component {
-    getNativeComponent;
     getMapManagerCommand;
     getUIManagerCommand;
     static Animated;
  1. Added the following script to package.json:
"scripts": {
    "postinstall": "patch -p1 < react-native-maps+1.20.1.patch node_modules/react-native-maps/lib/MapMarker.js",
}
  • Note that the file react-native-maps+1.20.1.patch should be in the same directory as package.json.

For anyone landing here recently: just go to node_modules/react-native-maps/lib/MapMarker.js:32 and delete: getNativeComponent;

Worked. Thanks!

it worked

I was able to fix the issue thanks to the instructions from this video. You can refer to the guidance in this video: https://www.youtube.com/watch?v=AjMV8S59v-Y Wishing you success!

For anyone landing here recently: just go to node_modules/react-native-maps/lib/MapMarker.js:32 and delete: getNativeComponent;

This one still working. Thnaks man.

Hello everyone, after reading all your comment and testing some of them this solution worked, not apply the plugins on google maps package:

module.exports = function (api) { api.cache(true);

return { presets: [ ‘module:metro-react-native-babel-preset’, // Required for React Native transformations ], plugins: [ ‘react-native-reanimated/plugin’, // Required for React Native Reanimated

  // Environment variable loading:
  [
    'module:react-native-dotenv',
    {
      moduleName: '@env',
      path: '.env',
      blocklist: null,
      allowlist: null,
      safe: false,
      allowUndefined: true,
    },
  ],
],
overrides: [
  {
    test: (fileName) => !fileName.includes('node_modules/react-native-maps'),
    plugins: [
      ['@babel/plugin-transform-class-properties', { loose: true }],
      ['@babel/plugin-transform-private-methods', { loose: true }],
      ['@babel/plugin-transform-private-property-in-object', { loose: true }],
    ],
  },
],

}; };

Try adding this in your babel.config.js

overrides: [{ test: fileName => !fileName.includes(‘node_modules/react-native-maps’), plugins: [[“@babel/plugin-transform-private-methods”, { “loose”: true }]] }],

post that delete node modules, yarn.lock, pods folder (ios), podfile.lock and then do yarn and pod It worked for me