Ad

How Do You Access An Object In An Array With This.state In React Native?

I am using react-native-snap-carousel and unable to access the URLs in the array. I can access them without other objects, but I need them to name each image.

      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>

I'm still new to react native and looking up similar questions didn't lead to the correct answer.

Ad

Answer

Here this.state.categoriesimages['images'], the data parameter accepts an array format.

Right now you're trying to access the categoriesimages with the index as images

Therefore you need to replace it with

data={this.state.categoriesimages}

and

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

Ad
source: stackoverflow.com
Ad