neohuiji
neohuiji

Reputation: 55

react-native-snap-carousel returns "Cannot read property 'concat' of undefined" when I use renderItme

I am learning React-Native and using react-native-snap-carousel in my project. I want to show some images but I got error Cannot read property 'concat' of undefined.

Here is my code.

const HomePage = (props) => {
   let imageList = ['../images/index-bg.png', '../images/down.png'];

   const renderImage = ({ item, index }) => {
       return (
            <View>
                <Image source={require(item)}></Image>
            </View>
        )
    };

   return (
     <View> 
            <Carousel
                loop={true}
                autoplay={true}
                data={imageList}
                renderItem={renderImage}
                sliderWidth={pxToDp(50)}
                itemWidth={pxToDp(100)}
            />

     </View>
   )

}

If I change

          <View>
                <Image source={require(item)}></Image>
            </View>

to

          <View>
                <Image source={'../images/index-bg.png'}></Image>
            </View>
···
it works well


I expect it would work like [Usage](https://github.com/archriss/react-native-snap-carousel) but it doesn't work.

Upvotes: 0

Views: 1680

Answers (1)

Marco Terzolo
Marco Terzolo

Reputation: 551

Did you try this?

let imageList = [require('../images/index-bg.png'), require('../images/down.png')];

and then

  <View>
    <Image source={item}></Image>
  </View>

Required source for images in RN have to be defined before runtime

Let me know if it works!

Upvotes: 1

Related Questions