- Create Components folder under src folder
- Add FetchEXP1.js file and add below Code
import React, { Component } from "react";
import { StyleSheet, View, Button } from "react-native";
export default class FetchEXP1 extends Component {
getDataUsingGet() {
fetch("https://jsonplaceholder.typicode.com/posts/1", {
method: "GET"
})
.then(response => response.json())
.then(responseJson => {
alert(JSON.stringify(responseJson));
console.log(responseJson);
})
.catch(error => {
alert(JSON.stringify(error));
console.error(error);
});
}
render() {
return (
<View style={styles.MainContainer}>
<Button title="Get Data Using GET" onPress={this.getDataUsingGet} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
justifyContent: "center",
flex: 1,
margin: 10
}
});
- Open App.js file and update code as below
import React from 'react';
import { View,StyleSheet } from 'react-native';
import FetchEXP1 from './src/Component/FetchEXP1'
const App=()=> {
return (
<View style={styles.container}>
<FetchEXP1/>
</View>
);
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
paddingTop:80
},
});
No comments:
Post a Comment