Render Content Dynamically from an array map function in React Native

前端 未结 3 1511
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 01:48

I\'m trying to get data from an array and using map function to render content. Look at

**{this.lapsList()}** 

and the associated

相关标签:
3条回答
  • 2020-12-09 02:13

    Don't forget to return the mapped array , like:

    lapsList() {
    
        return this.state.laps.map((data) => {
          return (
            <View><Text>{data.time}</Text></View>
          )
        })
    
    }
    

    Reference for the map() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

    0 讨论(0)
  • 2020-12-09 02:21

    Try moving the lapsList function out of your class and into your render function:

    render() {
      const lapsList = this.state.laps.map((data) => {
        return (
          <View><Text>{data.time}</Text></View>
        )
      })
    
      return (
        <View style={styles.container}>
          <View style={styles.footer}>
            <View><Text>coucou test</Text></View>
            {lapsList}
          </View>
        </View>
      )
    }
    
    0 讨论(0)
  • 2020-12-09 02:27
    lapsList() {
    
        return this.state.laps.map((data) => {
          return (
            <View><Text>{data.time}</Text></View>
          )
        })
    }
    

    You forgot to return the map. this code will resolve the issue.

    0 讨论(0)
提交回复
热议问题