我在我的应用程序中使用了react native mpaboxgl。
import MapboxGL from "@mapbox/react-native-mapbox-gl";
<MapboxGL.MapView
logoEnabled={false}
attributionEnabled={false}
ref={(e) => { this.oMap = e }}
zoomLevel={6}
centerCoordinate={[54.0, 24.0]}>
<MapboxGL.MapView>
如何在其他组件中使用oMap?因此,我可以在其他组件/页面上执行类似图层开/关的操作。
更新:
使用将起作用的全局变量。
ref={ref=>{
global.input=ref;
}}
现在,您可以global.input.focus()
在此屏幕之后在应用程序中的任何位置使用。
这是实现此目的的示例:https : //snack.expo.io/ryJk3hFKN
您可以创建一个函数,该函数返回该组件的引用。
并将该功能作为道具传递给其他组件
import * as React from 'react';
import { Text, View, StyleSheet, TextInput, TouchableOpacity } from 'react-native';
export default class App extends React.Component {
getInputRef = () => this.input;
render() {
return (
<View style={styles.container}>
<TextInput
ref={ref => {
this.input = ref;
}}
placeholder="Hi there"
/>
<SecondComp getInputRef={this.getInputRef} />
</View>
);
}
}
class SecondComp extends React.Component {
render() {
return (
<TouchableOpacity
onPress={() => {
this.props.getInputRef().focus();
}}>
<Text>click to Focus TextInput from the other component</Text>
</TouchableOpacity>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-around',
backgroundColor: '#ecf0f1',
padding: 8,
},
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句