如何在 react native 中访问嵌套的地图 ref?

问题描述 投票:0回答:1

我有一个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>
  )
}
react-native react-native-maps
1个回答
0
投票

试试这样的方法。

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>
  )
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.