How to display multiple images from api call - reactjs

image code
I need to display some images from api. I am using axios to get images. ids of images is coming from another api one by one. if i use map it will show nothing . I want to show all images as soon as api send the id. map pnly works on loading i guess.

You can use ListView in React
example:
<ListView
dataSource={this.state.dataSource}
renderRow={data => (
<View style={styles.container}>
<Image
source={{ uri: imageSource }}
style={styles.img}
/>
<Text>{data}</Text>
</View>)}
/>

Related

React Native Image displays different src url

In react native, the <Image> tag isn't displaying properly and shows an image from a different src URL in the database. I checked the URL multiple times, and it isn't the one displayed in the HTML.
<Image source={{uri: image}} style={styles.image} />
What is causing this glitch?
Your browser caches your url and assumes you are hitting the same url so it populates previous result. Add a random query to the url like:
https://source.com/a/b/c/image.png?randomQuery={random_number_here}
Example:
https://i.pinimg.com/564x/62/ed/6e/62ed6ea71018a57a3ab0c8c959d78cb0.jpg?key=1656566265232
React native:
<Image
source={{ uri: `${imageUrl}?key=${Date.now()}` }}
/>

React-Native -Elements Avatar not rendering and showing only gray background even though the image path is correct

I am trying to render the avatar in my listItem in my React_Native Application but the image is not rendering even though my image URI path is correct and it's only giving the gray background.
Here is my code
<View>
{props.dishes.map((l, i) => (
<ListItem key={i} bottomDivider>
<Avatar rounded source={{ uri: l.image }} />
<ListItem.Content>
<ListItem.Title>{l.name}</ListItem.Title>
<ListItem.Subtitle>{l.description}</ListItem.Subtitle>
</ListItem.Content>
</ListItem>
))}
</View>
Here is the image of what I am getting
I want the images to be rendered there but it's showing only a grey background. I also tried putting the URI of online images but it's giving the same result.
I also followed the solutions mentioned here but it is not working.
I also passed the online uri to check if my path uri is incorrect but it the same result
<Avatar
source={{
uri:"https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg",
}}
/>
I solve the issue as passing the path of the image like this was not working
Bypassing an image path like this was rendering only grey background.
<Avatar rounded source={{ uri: "./images/uthappizza.png"}} />
This is how I passed the image path and its rendering properly by using require()
<Avatar rounded source={require("./images/uthappizza.png")} />
I tried several ways, until I found a guy who encouraged me to check emulator connectivity ... and that was the problem!!
To solve it, before running your app, launch emulator with
emulator -avd <your-emulator-name> -dns-server 8.8.8.8

ReactJs: how can i render binary (base64) image format

i have images in sqlserver as a binary, how can i display them in reactJs?
i tried the following but doesn't work
<Image source={{ uri: `data:image/jpeg;base64,${results.student_photo}` }} />
i also tried
const base64Image = {results.student_photo};
<Image source={{uri: `data:image/jpeg;base64,${base64Image}`}} />
This may answer your question.
How to display binary data as image in React?
Hope this helps.

How to do WebView in react native?

I've tried different methods to render an html content from json response to react native.But I got failed in these methods
https://www.npmjs.com/package/react-native-render-html
https://www.npmjs.com/package/react-native-htmlview
Because there is no package that support table tag of html. So I thought to use WebView in react native.But that also disappointed me. There is no field to give html content from json response in WebView. I've tried the following.
But these are not working
code
<WebView
source={{uri: `<h1>Hello World</h1>`}}
style={{marginTop: 20}}
/>
In my case I'm fetching server response.I've double checked whether response is coming ,the response from server is fine.
return (
<Container style={styles.container}>
<View>
{this.state.section.map(article =>
<View>
<WebView
source={{uri:article.data.description}}
style={{marginTop: 20}}
/>
</View>
)}
</View>
</Container>
);
Is there any other method for rendering html content other than these. Please I'm stuck with this.I don't know what to do.This cause delay in my project.So I'm really disappointed.Please any help would be really appreciable.
I've solved the problem.I was using native-base ui kit.So when I tried to render WebView within Container or Content it won't render.So I removed it with View component but ScrollView don't work.So I've made some changes to Container and it is working perfect.But still remains a problem that border of html table is not displaying.But otherwise it is perfect
This is my working code
<Content contentContainerStyle={{ flex: 1 ,padding:15}}>
{this.state.section.map(article =>
<WebView
source={{ html: article.data.description}}
javaScriptEnabled={true}
style={{backgroundColor: 'transparent'}}
/>
)}
</Content>

How to run a function in react native when you press on a picture?

I am trying to run a function on press of a picture in react native.
<Image source={mysource} onPress={()=>{Alert.alert("hello")}} />
Am I supposed to do it like this? It doesnt work. I think I should wrap the image by button tags but I am not sure how to do it. Is there any best way to do this?
Yes you need to wrap inside the Touchable property like this:
<TouchableHighlight onPress={() => {Alert.alert("hello")}}>
<Image source={mysource} />
</TouchableHighlight>

Resources