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

前端 未结 2 1747
孤城傲影
孤城傲影 2021-02-07 10:43

I have this array which contains objects:

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

Now in the

相关标签:
2条回答
  • 2021-02-07 11:17

    Try forEach method instead of map:

       var WholeNews =[];
       tmp_array.forEach(function(news, i){
          WholeNews.push(
            <View key={i}>
              <Text>{news.headline}</Text>
              <View>
                <Text>{news.text}</Text>
              </View>
            </View>);
        });
    

    And note from where you can get the i index..

    0 讨论(0)
  • 2021-02-07 11:20

    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>
      )
    }
    
    0 讨论(0)
提交回复
热议问题