Javascript 使用fetch call函数从另一个页面返回结果值,React native
我需要从react native中的另一个页面返回函数的结果,该页面执行fetch调用。我使用的方法如下。我知道这是因为异步调用。在react native中是否有实现这一点的特殊方法 fetchcall.jsJavascript 使用fetch call函数从另一个页面返回结果值,React native,javascript,react-native,async-await,fetch,Javascript,React Native,Async Await,Fetch,我需要从react native中的另一个页面返回函数的结果,该页面执行fetch调用。我使用的方法如下。我知道这是因为异步调用。在react native中是否有实现这一点的特殊方法 fetchcall.js import address from '../actions/address' const dashboard = { getvals(){ return fetch(address.dashboardStats(), {method: "POST",
import address from '../actions/address'
const dashboard = {
getvals(){
return fetch(address.dashboardStats(),
{method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify( {...
}),
})
.then((response) => response.json())
.then((responseData) => {
console.warn(responseData);
return responseData;
})
.catch((error) => { console.warn(error); })
.done();
// return 'test_val'';
}
}
export default dashboard;
import dashboard from '../../services/dashboard';
class Dashboard extends Component {
componentDidMount(){
console.warn(dashboard.getvals());
}
}
export default connect(mapStateToProps, bindAction)(Dashboard);
dashboard.js
import address from '../actions/address'
const dashboard = {
getvals(){
return fetch(address.dashboardStats(),
{method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify( {...
}),
})
.then((response) => response.json())
.then((responseData) => {
console.warn(responseData);
return responseData;
})
.catch((error) => { console.warn(error); })
.done();
// return 'test_val'';
}
}
export default dashboard;
import dashboard from '../../services/dashboard';
class Dashboard extends Component {
componentDidMount(){
console.warn(dashboard.getvals());
}
}
export default connect(mapStateToProps, bindAction)(Dashboard);
它将结果显示为“undefined”,但该fetch调用起作用并显示结果。有什么建议吗 在fetchcall.js中,您将返回一个承诺。另外,由于您在
.then()
方法本身中返回responseData
,因此不需要.done()
方法
由于getvals()
正在返回承诺,因此需要在方法中访问它的值
总的来说,您的代码应该如下所示:
函数getvals(){
返回获取('https://jsonplaceholder.typicode.com/posts',
{
方法:“获取”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,
},
})
.then((response)=>response.json())
.然后((响应数据)=>{
控制台日志(responseData);
返回响应数据;
})
.catch(错误=>console.warn(错误));
}
然后(response=>console.log(response))代码>我认为,最好的架构模式是使用回调函数,通常是作为匿名函数写入
///Component.js
my_service.login((data=>{
this.setState({body: data.body});
}));
////Service.js
export const login = function (cb){
fetch('http://myapi.com/103?_format=hal_json')
.then((response) =>{
return response.json();
})
.then((data) =>{
cb(data);
});
}
我还是一名初级开发人员,但经常使用这种模式。如果有人有不同方法的理由,我很乐意听到 fetch始终返回承诺与您返回的内容无关。
因此,您可以返回字符串、变量或其他内容,但必须使用。然后使用访问数据
发出提取请求的文件
export const graphql = () => {
return fetch("https://graphqlzero.almansi.me/api", {
"method": "POST",
"headers": { "content-type": "application/json" },
"body": JSON.stringify({
query: `{
user(id: 1) {
id
name
}
}`
})
})
.then((res) => res.json()).then((responseData) => {
console.log(responseData);
return responseData;
})
}
调用函数的文件
graphql()
.then((e) => {
console.log("data", e)
});
您确定它是getTestval
而不是getvals
?@akond,很抱歉,编辑问题时出错了。我现在编辑了它。我看到您正在使用react redux connect,但我根本看不到MapStateTops定义。你在用它吗?你会考虑一个没有ReDux的解决方案吗?抱歉,我知道这是旧的,但是为什么在<代码>控制台> WAN()/>代码>返回<代码>响应数据< /代码>。返回它是否有问题,或者它应该是console.log()
?它应该是console.log()
。我会更改它。请告诉我如何传递,我想导出函数getvals(){return fetch(')。然后((response)=>response.json())。然后((json)=>{return json.products;})。catch((error)=>{console.error(error);}getvals()。然后(response=>response);const ProductsList=getvals()。然后(response=>response);导出默认产品列表;如何不仅使用数据
,而且使用响应
?