Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

react-native

How can I get this to work, {showExamples} isn't showing any output. The full source code is here: https://rnplay.org/apps/t2E4Ig

var MyApp = React.createClass({
  render() {
    var showExamples = examples.map(function(value){
                return (
            <View>
                {value.render}
            </View>
          );
            });

    return (
        <View>

        <Image
          source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
          style={styles.base}
        />

        {showExamples}

        </View>
    );
  }
});
like image 827
Giant Elk Avatar asked Nov 04 '15 04:11

Giant Elk


People also ask

How map function works in react native?

In React, the map method is used to traverse and display a list of similar objects of a component. A map is not a feature of React. Instead, it is the standard JavaScript function that could be called on an array. The map() method creates a new array by calling a provided function on every element in the calling array.

How do I print a map in React?

The rendered Maps can be printed directly from the browser by calling the print method. To use the print functionality, the Print module must be injected into the Maps using Inject services={[]} tag and set the allowPrint property to true.


2 Answers

you can try this.

var MyApp = React.createClass({
  render() {

   return (
    <View>

      <Image
        source={{uri: 'http://facebook.github.io/react/img/logo_og.png'}}
      style={styles.base}
      />

     {
       examples.map(function(value, i){
            return (
              <View key={i}>
                {value.render}
             </View>
           );
        })
     }

    </View>
);

} });

like image 187
m4l3 Avatar answered Sep 23 '22 19:09

m4l3


The render property on each of the example items is a function, so you need to invoke it with {value.render()}, instead of trying to "render a function: with {value.render}.

like image 36
jevakallio Avatar answered Sep 24 '22 19:09

jevakallio