Cannot call map method (showAttribution) - gives this._runNativeCommand is not a function
See original GitHub issueDescribe the bug
Cannot run showAttribution method on the map ref in a functional component. May also apply to other methods.
import React, { useRef } from 'react';
import { TouchableOpacity } from 'react-native';
import MapboxGL from '@react-native-mapbox-gl/maps';
export default function Map(p) {
const mapRef = useRef()
return (
<TouchableOpacity onPress={mapRef?.current?.showAttribution}>
<MapboxGL.MapView
ref={mapRef}
attributionEnabled={false}
/>
</TouchableOpacity>
)
}
Expected behavior
Opens attribution menu, as when you click the default ‘i’.
Actual behavior
Gives the following error:
this._runNativeCommand is not a function. (In 'this._runNativeCommand('showAttribution', this._nativeRef)', 'this._runNativeCommand' is undefined)
Versions (please complete the following information):
- Platform: Android
- Platform OS: Android 8
- Device: Pixel 2
- Emulator/ Simulator: Yes
- Dev OS: Win10 Professional
- react-native-mapbox-gl Version: 8.2.0
- Mapbox GL version: default
- React Native Version: 0.64.0
Update
I’ve created a fresh React Native project (latest at 0.64.2) using the latest master from this package. I’m still experiencing issues with refs, where the ref is not populated.
react-native init MapTest
npm i -S https://github.com/react-native-mapbox-gl/maps
app.js:
import React, {useRef} from 'react';
import type {Node} from 'react';
import Mapbox from '@react-native-mapbox-gl/maps';
import {View} from 'react-native';
Mapbox.setAccessToken(
'pk.xxx'
);
const App: () => Node = () => {
const mapRef = useRef();
console.log('mapRef =', mapRef);
return (
<View style={{flex: 1}}>
<Mapbox.MapView ref={mapRef} style={{width: '100%', height: '100%'}} />
</View>
);
};
export default App;
The map loads, but mapRef = {"current": undefined}
is logged and no methods are accessible.
Interestly, if you hit CTRL+S on App.js, this causes the app to fast refresh and then mapRef.current methods become available. But they aren’t available during normal use of the app.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I am still getting this exact issue, has any solution been found?
@ferdicus There is a difference in the mapbox v10 implementation - the
showAttribution
method is missing in the View Manager.https://github.com/rnmapbox/maps/blob/main/ios/RCTMGL-v10/RCTMGLMapViewManager.m vs. https://github.com/rnmapbox/maps/blob/main/ios/RCTMGL/RCTMGLMapViewManager.m#L292
But it looks like that even in the old implementation the referenced view is missing the method as well. So no idea why it worked in the old mapbox version.