I'm currently doing the react native movies tutorial (https://facebook.github.io/react-native/docs/tutorial.html), and I am using a device to display the results. I used an existing project, put a view using storyboard, and subclassed it correctly. For some reason, the image is not being displayed, and a red box is being shown instead. Am I doing something wrong? My React code:
'use strict';
var React = require('react-native');
var {
AppRegistry,
Image,
StyleSheet,
Text,
View,
} = React;
var MOCKED_MOVIES_DATA = [
{title: 'Title', year: '2015', posters: {thumbnail: 'http://i.imgur.com/UePbdph.jpg'}},
];
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
thumbnail: {
width: 53,
height: 81,
},
});
var Movies = React.createClass({
render: function() {
var movie = MOCKED_MOVIES_DATA[0];
return (
<View style={styles.container}>
<Text>{movie.title}</Text>
<Text>{movie.year}</Text>
<Image
source={{uri: movie.posters.thumbnail}}
style={styles.thumbnail}
/>
</View>
);
}
});
React.AppRegistry.registerComponent('Movies', () => Movies);
This is a screenshot of what is being displayed on my phone:
To load images in react native from URL you must specify the image width, and height. React native cannot determine the width and height from downloaded images. After specifying width, and height we can specify the URL to the image source prop. Inside this prop we will specify a uri which holds our image URL.
Image In Class Component In React Native To browse the image from the local disk first create a folder and put the image in that folder. 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.
To fetch image from API with React, we can use the fetch function. We call fetch with the imageUrl to make a GET request to it. Then we call res. blob to convert the response object to a blob.
Just write https instead of http and it will work. But it will only work if the server accepts https. Otherwise change the info.plist file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With