Javascript 如何获取从另一个组件返回的数据
我需要获取一个api并从React native中的一个文件中获取这些数据,我能够通过console.log()获取获取api的文件中的数据 API.jsJavascript 如何获取从另一个组件返回的数据,javascript,reactjs,react-native,async-await,Javascript,Reactjs,React Native,Async Await,我需要获取一个api并从React native中的一个文件中获取这些数据,我能够通过console.log()获取获取api的文件中的数据 API.js 商户列表.js 我在网上做了一些研究,发现可能是aysnc和Wait的问题,但我不知道如何修改代码 任何帮助都将不胜感激 修改您的函数,如下所示 export const merchastListFetch = ()=>{ return axios.get('http://myapi.com/API/fetch_all')
商户列表.js
我在网上做了一些研究,发现可能是aysnc和Wait的问题,但我不知道如何修改代码
任何帮助都将不胜感激 修改您的函数,如下所示
export const merchastListFetch = ()=>{
return axios.get('http://myapi.com/API/fetch_all')
.then(function (response) {
return response.data;
}).catch((error)=>{
console.log(error)
})
}
您可以将响应保存为数组状态,然后在需要的地方调用和修改它 大概是这样的:
async CallApiFunc(){
await fetch("http://myapi.com/API/fetch_all")
.then(res => res.json())
.then(res => {
console.log(JSON.stringify(res))
this.setState({
dataArray: res
})
})
.catch(err => console.log(err))
}
这需要一个dataArray:[]
作为状态
之后,您可以在一个平面列表中显示入口,例如,也可以像修改state一样修改它们。它向我显示了“意外令牌”错误,在第2行返回时,您可以提供此路由的后端定义吗
http://myapi.com/API/fetch_all
甚至是存储数据(如果需要存储的话)
{"status":true,"data":[{"shop_id":"1","merchant_id":"1","area_id":"0","agent_id":"1","business_type_id":"1","business_category_id":"1","bill_type_id":"1","currency_id":"0","status_id":"0","register_name":"Dummy Name","register_no":"123456789","retail_name":"Test Only","description":"TESTING USE","commission":"20.00","gst_no":"12345","coming_soon":"0","is_halal":"0","is_active":"1","delivery_charge":"3.50","remarks":"TESTING USE AGAIN","approved_date":"0000-00-00 00:00:00","deleted":"0","created_date":"2020-10-06 15:02:20","created_by":"1","modified_date":"2020-10-08 09:37:53","modified_by":"1","merchant":"Merchant","shop_image":[{"shop_image_id":"3","shop_id":"1","file":"\/images\/shop\/5af16e1c6554160a79bea005.png","file_size":"65124","file_type":"image\/png","is_default":"1","is_active":"1","deleted":"0","created_date":"2020-10-09 13:21:23","created_by":"0","modified_date":"0000-00-00 00:00:00","modified_by":"0"}]},
export const merchastListFetch = ()=>{
return axios.get('http://myapi.com/API/fetch_all')
.then(function (response) {
return response.data;
}).catch((error)=>{
console.log(error)
})
}
async CallApiFunc(){
await fetch("http://myapi.com/API/fetch_all")
.then(res => res.json())
.then(res => {
console.log(JSON.stringify(res))
this.setState({
dataArray: res
})
})
.catch(err => console.log(err))
}