Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image passed as props react native

Tags:

react-native

When I am trying to load image from props i am getting the following error warning: failed prop type: invalid prop source supplied to image Here is my code

src/components/common/Header.js

<View style={viewStyle}>
  <Text style={textStyle}>{props.children}</Text>
  <Image style={{height:25, width:25}} source={props.imageUri} />
</View>

src/index.js

<Header imageUri='../../images/logout.png'>My App</Header>

and the image is at path src/images/logout.png

please help

like image 631
Raj Suvariya Avatar asked Mar 21 '17 09:03

Raj Suvariya


People also ask

Can you load an image in React Native?

Background images in React Native CSS is typically the language used to add background images, but React Native provides an ImageBackground component that makes similar functionality available in web applications. The ImageBackground component also accepts the same props that the Image component accepts.

How do I import an image into a folder in React Native?

Image In Class Component In React Native You can copy and paste from other folder. After that provide the path of that image inside the require which is a property of source of Image tag. Simply create an assets folder and paste the images there.

How do you pass components as props in React Native?

You can pass a component as props in React by using the built-in children prop. All elements you pass between the opening and closing tags of a component get assigned to the children prop. Copied!


5 Answers

As the React Native Documentation says, all your images sources need to be loaded before compiling your bundle.

Adding a simple image in React Native should be like this:

<Image source={require('./path_to_your_image.png')} />

Let's say you have this:

const slides = {
  car: './car.png',
  phone: '../phone.png',
}

Then you pass slides as a parameter but still, you won't be able to use it like this (even though logically should work):

<Image source={require(props.car)} />

Use require() inside the sildes{}

const slides = {
  car: require('./car.png'),
  phone: require('../phone.png'),
}

And then use it like this:

<Image source={props.car}/>
like image 131
abranhe Avatar answered Oct 19 '22 20:10

abranhe


you can set source of image from props like this : in component :

<Image source={props.path} > 

and pass source to component like this: in parent :

<
.
.
path={require("../assets/images/icon.svg")}
.
.
>
like image 31
mahsa k Avatar answered Oct 19 '22 21:10

mahsa k


your source is wrong.

It should use uri properties.

Either you use require:

<Image source={require('.../../images/logout.png')} />

in turn, you can then require this prop too

<Image source={require(props.imageUri)} />

Or you use the Uri as is:

<Image source={{uri: props.imageUri }} />

Refer to the documentation for more details here

like image 36
Tikkes Avatar answered Oct 19 '22 22:10

Tikkes


Using the below method gives error in react-native:

<Image style={{height:25, width:25}} source={require(props.imageUri)} />

Instead use:

<Image style={{height:25, width:25}} source={props.imageUri}/>

And pass the below code as imageUri prop from the Parent component.

require('./public/images/image.png')

It has worked for me and hope this will help others.

like image 45
Shreyak Upadhyay Avatar answered Oct 19 '22 22:10

Shreyak Upadhyay


For a local image path the syntax is

<Image style={{height:25, width:25}} source={require(props.imageUri)} />
like image 40
Hariks Avatar answered Oct 19 '22 21:10

Hariks