Render Content Dynamically from an array map function in React Native

Xavier C.

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

**{this.lapsList()}** 

and the associated

**lapsList()** 

function to understand what I'm trying to do. The result is nothing is displaying (Views under view, etc.) Here is my simplified code:

class StopWatch extends Component {

constructor(props) {
  super(props);

  this.state = {
    laps: []
  };
}

render() {
  return (
    <View style={styles.container}>
        <View style={styles.footer}>
          <View><Text>coucou test</Text></View>
          {this.lapsList()}
        </View>
    </View>
  )
}

lapsList() {

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

}

_handlePressLap() {

  console.log("press lap");

  if (!this.state.isRunning) {

    this.setState({
      laps: []
    })

    return

  }

  let laps = this.state.laps.concat([{'time': this.state.timeElapsed}]);

  this.setState({
      laps: laps
  })

  console.log(laps);

}

}

Ivan Chernykh

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Dynamically building a render function in React

From Dev

React render array returned from map

From Dev

Can't get .map() working in React Native render()

From Dev

React Native render function throws error

From Dev

React-native, render a button click dynamically

From Dev

Render items from 2D Array in React Native JSX

From Dev

How to render a modal with map() from arraylist in react

From Dev

Using Map function over an array of objects inside React native not working

From Dev

map is not a function on array React

From Dev

Unable to render array of objects in react-native

From Dev

Dynamically add strings from array content

From Dev

Skipping Views in React Native render function

From Dev

React-native, render a button click dynamically

From Dev

react native dynamically change ListView from SQLite

From Dev

TouchableHighLight onPress inside a map function in react native

From Dev

Stringifying all array content with .map function

From Dev

Dynamically add Keys From Array Content

From Dev

.map() function inside ternary in react native jsx

From Dev

How to fetch content from an array of URLs react native

From Dev

Pass function from props to render function in react

From Dev

Can't render from array map on const

From Dev

react-native function in map

From Dev

React with Typescript: Render components from `map` function

From Dev

Redux on React Native not able to render from reducer

From Dev

Map through an array and render a specified component in React

From Dev

React Native call a function in a render method

From Dev

react router - how to render Link from content

From Dev

How to render components using function in React Native

From Dev

In React Native how to get key in flatlist dynamically from json array

Related Related

  1. 1

    Dynamically building a render function in React

  2. 2

    React render array returned from map

  3. 3

    Can't get .map() working in React Native render()

  4. 4

    React Native render function throws error

  5. 5

    React-native, render a button click dynamically

  6. 6

    Render items from 2D Array in React Native JSX

  7. 7

    How to render a modal with map() from arraylist in react

  8. 8

    Using Map function over an array of objects inside React native not working

  9. 9

    map is not a function on array React

  10. 10

    Unable to render array of objects in react-native

  11. 11

    Dynamically add strings from array content

  12. 12

    Skipping Views in React Native render function

  13. 13

    React-native, render a button click dynamically

  14. 14

    react native dynamically change ListView from SQLite

  15. 15

    TouchableHighLight onPress inside a map function in react native

  16. 16

    Stringifying all array content with .map function

  17. 17

    Dynamically add Keys From Array Content

  18. 18

    .map() function inside ternary in react native jsx

  19. 19

    How to fetch content from an array of URLs react native

  20. 20

    Pass function from props to render function in react

  21. 21

    Can't render from array map on const

  22. 22

    react-native function in map

  23. 23

    React with Typescript: Render components from `map` function

  24. 24

    Redux on React Native not able to render from reducer

  25. 25

    Map through an array and render a specified component in React

  26. 26

    React Native call a function in a render method

  27. 27

    react router - how to render Link from content

  28. 28

    How to render components using function in React Native

  29. 29

    In React Native how to get key in flatlist dynamically from json array

HotTag

Archive