0
votes

I am trying to display a simple .jpg image by sending the Image path as a prop to the component which is supposed to render it. In the below way.

App.js

import React, { Component } from 'react';
import { View, Text, Image } from 'react-native';
import  Header from './components/Header';
import ImageSlider from './components/ImageSlider';
import ImageShow from './components/ImageShow';

class App extends Component {
    render () {
        return (
            <View style={{flex:1}}>
                <Header headerText = "HONEST REVIEWS" />
                <ImageSlider  />
                <ImageShow   imagePath = "./abc.jpg"/>
                <ImageShow   imagePath = "./abc.png" />

            </View>
        );
    }
}

export default App;

ImageShow.js

import React from 'react';
import { View, Image, Dimensions } from 'react-native';


const widthOfScreen = Dimensions.get('window').width;

const ImageShow = (imageProps) => {

    return (
        <View>
            <Image  style = { {width: 50, height: 50} } source = { {uri: imageProps.imagePath} } /> 
        </View>

    );

};

const styles = {
    ImageStyle : {
        height: 30,
        width: widthOfScreen
    }
}

export default ImageShow;

ImageSlider.js

import React from 'react';
import Carousel from 'react-native-banner-carousel';
import { StyleSheet, Image, View, Dimensions } from 'react-native';

const BannerWidth = Dimensions.get('window').width;
const BannerHeight = 250;

const images = [
    require('./abc.jpg'),
    require('./abc.jpg'),
    require('./abc.jpg')
];

export default class ImageSlider extends React.Component {
    renderPage(image, index) {
        return (
            <View key={index}>
                <Image style={styles.imagesInSlider} source = { image } /> 
            </View>
        ); 
    }

    render() {
        return (
            <View style={styles.container}>
                <Carousel
                    autoplay
                    autoplayTimeout={2000}
                    loop
                    index={0}
                    pageSize={BannerWidth}
                >
                    {images.map((image, index) => this.renderPage(image, index))}
                </Carousel>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#fff',
        justifyContent: 'flex-start',
        alignItems: 'center'

    },
    imagesInSlider: {
        width: BannerWidth,
        height: 250,
        resizeMode: 'stretch',
    }
});

My folder structure is :

ProjectName
------src
--------components
-----------ImageShow.js
-----------ImageSlider.js
-----------Header.js
-----------abc.jpg
--------App.js

Ideally the Image should be displayed when I am passing the locally stored Image path, but I am not getting any Image displayed but a Warning message which says: "failed prop type: invalid prop 'source' supplied to 'ForwardRef(image)'"

1
use {uri: imageProps.imagePath }Just code
It didn't worked for me. I have edited and pasted the complete code snippet above, just have a look at it once, it says the "could not find Image", but even I change the ImagePath still there are some styling issues which prevent me from showing the image.mshikher

1 Answers

0
votes

This code work for me to get the image

import image1 from '../assets/images/abc.png'
import image2 from '../assets/images/abc.png'

const images = [
{
    name: image1
},
{
    name: image2
}]

const RenderPage = ({ image }) => {
 return (
    <Image resizeMode="contain" style={{ width: 50, height: 100, marginBottom: 5 }} source={image} />
 )
}

class myclass extends Component {
 render() {

    return (

        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
            {images.map((image, index) =>
                <RenderPage key={index} image={image.name} />
            )}
        </View>

    )
 }
}

Here the screenshot: enter image description here