如何使用React Native中的this.state访问数组中的对象?

时间:2018-04-07 14:37:13

标签: reactjs react-native react-native-snap-carousel

我使用的是react-native-snap-carousel,无法访问数组中的URL。 我可以在没有其他对象的情况下访问它们,但我需要它们来命名每个图像。

      constructor(props)
  {

    super(props);

    this.state = {
    categoriesimages: [
 {
   name: 'business 1',
   images:
     'http://www.example.com/img/venture.jpg',
 },
 {
   name: 'business 2',
   images:
     'http://www.example.com/img/venture.jpg',
 },
 {
   name: 'business 3',
   images:
     'http://www.example.com/img/venture.jpg',
 }
]

  }
 renderItem = ({ item }) => {
       return (
           <Image source={{ uri: item }} style={styles.logoStyle} />

       );
   }
<View>
                 <Carousel
                   inactiveSlideOpacity={0.6}
                   inactiveSlideScale={0.65}
                   firstItem={1}
                   sliderWidth={width}
                   itemWidth={width / 3}
                   data={this.state.categoriesimages['images']} // this is the problem
                   renderItem={this.renderItem}
                   containerCustomStyle={{ overflow: 'visible' }}
                   contentContainerCustomStyle={{ overflow: 'visible' }}
                 />
             </View>

我仍然不熟悉原住民,并且查找类似的问题并没有得出正确的答案。

2 个答案:

答案 0 :(得分:1)

此处this.state.categoriesimages['images']data参数接受array格式。

目前您尝试访问categoriesimages,索引为images

因此您需要将其替换为

data={this.state.categoriesimages}

renderItem={({item}) => console.log(item.images)}

答案 1 :(得分:0)

您可以使用this.state.categoriesimage.map(value => value.image)

的地图