Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native mapping through array with object childs - working different as in react web?

I have this array which contains objects:

var tmp_array = [
 { headline: "Test", text: "Test text", send_at: "test date" }
];

Now in the web react environment I was able to map through this array and return its values to a variable which can then be rendered.

So I used the same approach:

    var i = -1;
    var WholeNews = tmp_array.map(function(news){
      i++;
      return(
        <View key={i}>
          <Text>{news.headline}</Text>
          <View>
            <Text>{news.text}</Text>
          </View>
        </View>);
    });

After the mapping has finished it should be rendered as following:

return(
  <View>
     {WholeNews}
  </View>
);

Unfortunately I receive a warning in my iOS Simulator which says:

Objects are not valid as a React child(found: object with keys {WholeNews}). If you meant to render a collection of children, use an array instead or wrap the object using createFrament(object) from the React-addons.

I am not sure whether I am missing out something completely or react native simply doesn't support a mapping through an array as in my example.

like image 330
noa-dev Avatar asked Feb 09 '16 07:02

noa-dev


1 Answers

This should work:

WholeNews() {
  return tmp_array.map(function(news, i){
    return(
      <View key={i}>
        <Text>{news.headline}</Text>
        <View>
          <Text>{news.text}</Text>
        </View>
      </View>
    );
  });
}

render() {
  return(
    <View>
      {this.WholeNews()}
    </View>
  )
}
like image 126
Samuli Hakoniemi Avatar answered Oct 05 '22 21:10

Samuli Hakoniemi