我有一个react-native-maps组件,向下多层嵌套。我如何从父容器中访问map.animateToRegion函数?
class Parent extends Component{
animateToRegion = () => {
/*Need some way to call the mapview ref*/
}
return(
<Child />
)
}
class Child extends Component{
return(
<MapView ref={map => this.map = map}>
</MapView>
)
}
试试这样的方法。
class Parent extends Component{
animateToRegion = () => {
this.map.animateToRegion(yourRegionObj, 100);
}
return(
<Child
mapRef={map => this.map = map}
/>
)
}
class Child extends Component{
return(
<MapView ref={this.props.mapRef}>
</MapView>
)
}