Search code examples
javascriptreactjsreact-nativereact-native-snap-carousel

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.

Solution

  • 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!