访问平面列表上的Redux存储数组值React Native

时间:2018-10-28 11:07:05

标签: arrays react-native redux react-redux react-native-flatlist

我想访问redux存储阵列中要显示在FlatList中的图像文件名,但是我不确定如何访问所有文件名数组值。

这是我的初始状态

var InitialState = Record({
    query : null,
    searchResults: new (Record({
        total: null,
        lastPage: null,
        items : [],
        perPage : null,
        currentPage : null,
        nextPageUrl: null,
        prevPageUrl:null,
    }))(),
})

items数组看起来像这样

[ { id: 1050,
user_id: null,
name: 'Item A',
description: null,
image :
{
   id : 150,
   filename : 'imageA.jpg'
}
},
{ id : 1051,
user_id: null,
name: 'Item A',
description: null,
image :
{
   id : 152,
   filename : 'imageB.jpg'
}
},
......
]

这是我显示图片的平面列表

<View>
       ...
        <FlatList
          data={this.props.search.searchResults.items}
          renderItem={({item, index}) => this._renderItemInfo(item)}
          keyExtractor={(item, index) => item.id.toString()}
        />
      </View>

这是renderItemInfo函数

_renderItemInfo(item, index){
    return(
      <View style={styles.resultBox}>
      <View style={{flex:1, flexDirection: 'row'}}>
        <Image
          style={{width: 100, height: 100}}
          source={{uri: 'https://www.somewebsite.com/image/'+ this.props.search.searchResults.items[0].image.filename}}
        />
        <Text id={item.id} style={{paddingLeft:5}}>{item.id} {item.name}</Text>
      </View>
        <Divider/>
      </View>
    )
  }

在图像源上,如何访问所有文件名?

this.props.search.searchResults.items[0].image.filename

这仅访问第一个数组。

如果我将索引放在数组键上

this.props.search.searchResults.items[index].image.filename

出现此错误

  

TypeError:TypeError:null不是对象(正在评估   'this.props.search.searchResults.items [index] .image.filename')

有什么主意如何访问所有结果的图像文件名值?

1 个答案:

答案 0 :(得分:0)

您要传递<xsl:when test="position() = 1"> 作为平面列表的data属性,这意味着<xsl:apply-templates select="lb" />函数将作为其数组this.props.search.searchResults.items的参数传递给该数组的每个成员。

这意味着您在设置图像组件的uri时应该只能够访问_renderItemInfo