【问题标题】:How to access nested maps ref in react native?如何在本机反应中访问嵌套的地图参考?
【发布时间】:2020-01-30 20:38:50
【问题描述】:

我有一个 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>
  )
}

【问题讨论】:

  • 你的Child 组件,它只是用React.forwardRef 转发他们的引用

标签: react-native react-native-maps


【解决方案1】:

试试这样的:

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>
  )
}

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多