React native 使用钩子删除请求
当用户按下我定义项目的按钮时,我试图删除,但我没有这样做。你能告诉我什么时候按这个按钮,删除这个项目吗?在url中,您可以看到我在第一个get请求中使用的json,之后我想为delete请求提供操作按钮React native 使用钩子删除请求,react-native,api,React Native,Api,当用户按下我定义项目的按钮时,我试图删除,但我没有这样做。你能告诉我什么时候按这个按钮,删除这个项目吗?在url中,您可以看到我在第一个get请求中使用的json,之后我想为delete请求提供操作按钮 export default function Categories(){ const [data,setData]=useState({}); const categoriesUrl ="https://northwind.vercel.app/api/categ
export default function Categories(){
const [data,setData]=useState({});
const categoriesUrl ="https://northwind.vercel.app/api/categories"
const [name,setName]=useState([]);
const [description,setDescription]=useState([]);
const [id,setID]=useState([]);
const [status,setStatus]=useState([]);
useEffect(()=>{
fetch(categoriesUrl)
.then((response)=>response.json())
.then((json)=>{
setData(json);
})
.catch((error)=>console.error(error))
});
const deleteApi=(id)=>{
fetch("https://northwind.vercel.app/api/categories/"+id,{method: 'DELETE'})
.then(()=>setStatus('Delete Successful'));
}
return(
<SafeAreaView>
<View style={styles.container}>
<FlatList
data={data} keyExtractor={({id},index)=>id}
renderItem={({item})=>(
<View style={{paddingBottom:10}}>
<Text style={styles.categoriesText}>
{item.id}.{item.description},{item.name}
<Button onClick={()=>deleteApi(data.id)}></Button>
</Text>
</View>
)} >
</FlatList>
</View>
</SafeAreaView>
);
};
导出默认函数类别(){
const[data,setData]=useState({});
常量分类URL=”https://northwind.vercel.app/api/categories"
const[name,setName]=useState([]);
const[description,setDescription]=useState([]);
const[id,setID]=useState([]);
const[status,setStatus]=useState([]);
useffect(()=>{
获取(分类URL)
.then((response)=>response.json())
。然后((json)=>{
setData(json);
})
.catch((错误)=>console.error(错误))
});
常量deleteApi=(id)=>{
取回(“https://northwind.vercel.app/api/categories/“+id,{method:'DELETE'})
。然后(()=>setStatus('Delete Successful');
}
返回(
id}
renderItem={({item})=>(
{item.id}.{item.description},{item.name}
deleteApi(data.id)}>
)} >
);
};