【问题标题】:How to pass a state's value into props in React class component如何将状态的值传递给 React 类组件中的道具
【发布时间】:2020-10-12 14:37:57
【问题描述】:

我正在通过 axios 和 setState 将地理编码位置获取到 latt,lngg 变量中,我想在 google-map-react 包中心内使用这个变量我如何将这些状态值传递到 props 中。

休息 API

then(res => {
  console.log(res.data.results[0].geometry.location.lat);
  console.log(res.data.results[0].geometry.location.lng);
  // console.log(res.data[0]);

  this.setState({latt:res.data.results[0].geometry.location.lat})
  this.setState({lngg:res.data.results[0].geometry.location.lng})
})

static defaultProps = {
  center: {
    lat: 32,
    lng: 56
  },
  zoom: 5.7
};

<GoogleMapReact bootstrapURLKeys={{ key:API-KEY}} defaultCenter={this.props.center} defaultZoom={this.props.zoom}>

【问题讨论】:

    标签: reactjs google-maps google-map-react


    【解决方案1】:

    对于引用状态值,您将使用this.state.centerthis.state.zoom 而不是this.props.,后者用于当前组件的道具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2017-04-30
      • 2018-08-01
      • 2020-11-06
      • 1970-01-01
      • 2021-01-07
      • 2020-10-08
      相关资源
      最近更新 更多