Call API using (Axios)
app.js this project contains only app.js and image in assets folder
import React, { useEffect } from 'react';
import {
StyleSheet,
View,
Text,
FlatList,
SafeAreaView,
ActivityIndicator,
Image,
} from 'react-native';
import axios from 'axios';
const App = () => {
let [data, setData] = React.useState([]);
let [loading, setloading] = React.useState(true);
const myProducts = () => {
axios.get('http://alvibes.com/api/marketplace/bundledproducts')
.then(res => {
// console.log('my data>>>>>>>>>>>>>', res);
setData(res.data.data);
setloading(false);
})
.catch(function (error) {
alert(error);
});
}
useEffect(() => {
myProducts()
}, []);
if (loading) {
return (
<View>
<ActivityIndicator color="blue" size="large" />
</View>
);
}
return (
<View style={styles.box}>
<FlatList
horizontal={true}
data={data}
// keyExtractor={item => item.id.toString()}
// keyExtractor={(item)=> item.uri}
renderItem={({ item }) => (
<View style={styles.box2}>
<Text style={styles.text}>{item.title} </Text>
<Image
style={{ height: 200, width:200, alignSelf: 'center' }}
source={{ uri: item.images_thumb[0] }}
/>
<Text style={styles.text}>PKR:{item.sale_price}</Text>
<Image style={styles.boxImage} source={require('./assets/share.png')}/>
<Image style={styles.boxImage2} source={require('./assets/likeb.png')}/>
<Image style={styles.boxImage3} source={require('./assets/compare.png')}/>
</View>
)}
/>
</View>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'skyblue',
alignItems: 'center',
justifyContent: 'center',
},
loading: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
textAlign: 'center',
alignSelf: 'center',
color: 'black',
// marginTop: 0
borderBottomWidth:1
},
box: {
alignItems: 'center',
justifyContent: 'center',
marginTop: 10,
},
box2: {
backgroundColor: 'skyblue',
margin: 10,
padding: 20,
justifyContent: 'flex-start',
height: 340,
width:250,
alignItems: 'flex-start',
borderRadius:20
},
boxImage:{
position:'absolute',
height: 30,
width:30,
// elevation:-1,
marginTop:300,
marginLeft:10
},
boxImage2:{
position:'absolute',
height: 40,
width:40,
// elevation:-1,
marginTop:292,
marginLeft:195,
},
boxImage3:{
position:'absolute',
height: 40,
width:40,
// elevation:-1,
marginTop:292,
marginLeft:100
}
});
************************************************pkg.json
"dependencies": {
"react": "17.0.1",
"react-native": "0.64.2",
"axios": "^0.21.1"
},
Comments
Post a Comment