-
Notifications
You must be signed in to change notification settings - Fork 0
/
56_delete_and_data_rerender.js
75 lines (61 loc) · 1.64 KB
/
56_delete_and_data_rerender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React,{useEffect,useState} from 'react';
import {Platform,Button, TextInput,Text,StatusBar, View,ActivityIndicator,StyleSheet,Modal,Pressable,ScrollView,FlatList } from 'react-native';
import axios from 'axios';
import { get } from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
const App = () => {
const [data,setData]=useState([])
const getAPIData = async ()=>{
const url = "http://10.0.2.2:3000/users";
let result = await fetch(url);
result = await result.json();
console.warn(result);
if(result){
setData(result);
}
}
const deleteUser = async (id)=>{
const url = "http://10.0.2.2:3000/users/"+id;
let result = await fetch(url,{
method:'DELETE'
});
result = await result.json();
console.warn(result);
if(result){
console.warn('deleted');
getAPIData();
}
}
useEffect(()=>{
getAPIData();
},[])
return (
<View style={styles.container}>
{
data.length?
data.map((item)=>{
return (
<View style={styles.dataWrapper}>
<View style={{flex:1}}><Text>{item.name}</Text></View>
<View style={{flex:1}}><Text>{item.age}</Text></View>
<View style={{flex:1}}> <Button title='Delete' onPress={()=>deleteUser(item.id)} ></Button></View>
<View style={{flex:1}}> <Button title='Edit' ></Button></View>
</View>
)
}):null
}
</View>
);
};
const styles=StyleSheet.create({
container:{
flex:1,
},
dataWrapper:{
flexDirection:'row',
justifyContent:'space-around',
backgroundColor:'lightgrey',
margin:10,
padding:5
}
})
export default App;