Wait for react-promise to resolve before render

前端 未结 3 1573
误落风尘
误落风尘 2021-02-12 18:06

So I have a large set of data that I\'m retrieving from an API. I believe the problem is that my component is calling the renderMarkers function before the data is received from

3条回答
  •  鱼传尺愫
    2021-02-12 18:24

    You could do something like this to show a Loader until all the info is fetched:

    class Map extends Component {
      constructor () {
        super()
        this.state = { wells: [] }
      }
    
      componentDidMount() {
        this.props.fetchWells()
          .then(res => this.setState({ wells: res.wells }) )
      }
    
      render () {
        const { wells } = this.state
        return wells.length ? this.renderWells() : (
          Loading wells...
        )
      }
    }
    

提交回复
热议问题