首页 > 解决方案 > 在渲染中获取数据,但是在显示时,什么都没有出现

问题描述

我在反应原生方面面临着一个大问题。我有一个 react-native-maps 和一些标记。我希望当您单击标记时,您会获得一些数据并将其显示在标记中。但是我能够获取数据,但是当我显示它时什么都没有显示。但是,在渲染函数中,当我执行 console.log 时,我可以看到我的显示,所以我不明白为什么数据存在​​于渲染中而不是返回中,这对我来说毫无意义,我尝试了一切。 ..

这是我的代码。如果有人可以帮助我,那就太好了。谢谢 :)

我只是放了一个随机的免费api给你看

export class CustomMarker extends Component {
  constructor(props) {
    super(props)
    this.state = {
      bonjour: ""
    }
  }

  getData() {
    axios.get('https://dog.ceo/api/breeds/image/random').then((data) => {
      this.setState({ bonjour: data.data.message})
      //console.log(this.state.bonjour)
    })
  }

  render() {
    console.log(this.state.bonjour)
    return (

      <Marker key={this.props.id} coordinate = {{latitude: Number(this.props.data.dLocLati), longitude: Number(this.props.data.dLocLongi)}}
          pinColor = {"red"}
          title={this.props.data.iVehId}
          description={this.props.data.sLocStatus}
          onPress={() => this.getData()
          }
          >
      <Callout>
      <View>
        <Text>{this.state.bonjour}</Text>
      </View>
        </Callout>
      </Marker>
    )
  }
}

标签: javascriptreactjsreact-native

解决方案


推荐阅读