Return 从另一个页面获取调用函数的结果值,React native
Return the result value with fetch call function from another page, React native
我需要 return React Native 中执行提取调用的另一个页面的函数结果。我使用的方法如下。据我所知,这是因为异步调用。有没有一种特殊的方法可以在本机反应中实现这一点?
fetchcall.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;
dashboard.js
import dashboard from '../../services/dashboard';
class Dashboard extends Component {
componentDidMount(){
console.warn(dashboard.getvals());
}
}
export default connect(mapStateToProps, bindAction)(Dashboard);
它显示的结果为 "undefined",但是 fetch 调用有效并且它显示了结果。有什么建议吗?
在 fetchcall.js 中,您正在返回一个 Promise。此外,由于您在 .then()
方法本身中返回 responseData
,因此您不需要 .done()
方法。
由于 getvals()
正在返回一个 Promise,您需要在 .then()
方法中访问它的值。
总的来说,你的代码应该是这样的:
function getvals(){
return fetch('https://jsonplaceholder.typicode.com/posts',
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
return responseData;
})
.catch(error => console.warn(error));
}
getvals().then(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);
});
}
我还是一名初级开发人员,但经常使用这种模式。如果有人有理由采用不同的方法,我很乐意听到。
始终获取 return Promise 与您 returning 无关。
所以你可以 return 一个字符串,变量或其他东西但是你必须使用 .then 来访问数据
您发出提取请求的文件
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)
});
我需要 return React Native 中执行提取调用的另一个页面的函数结果。我使用的方法如下。据我所知,这是因为异步调用。有没有一种特殊的方法可以在本机反应中实现这一点?
fetchcall.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;
dashboard.js
import dashboard from '../../services/dashboard';
class Dashboard extends Component {
componentDidMount(){
console.warn(dashboard.getvals());
}
}
export default connect(mapStateToProps, bindAction)(Dashboard);
它显示的结果为 "undefined",但是 fetch 调用有效并且它显示了结果。有什么建议吗?
在 fetchcall.js 中,您正在返回一个 Promise。此外,由于您在 .then()
方法本身中返回 responseData
,因此您不需要 .done()
方法。
由于 getvals()
正在返回一个 Promise,您需要在 .then()
方法中访问它的值。
总的来说,你的代码应该是这样的:
function getvals(){
return fetch('https://jsonplaceholder.typicode.com/posts',
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
return responseData;
})
.catch(error => console.warn(error));
}
getvals().then(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);
});
}
我还是一名初级开发人员,但经常使用这种模式。如果有人有理由采用不同的方法,我很乐意听到。
始终获取 return Promise 与您 returning 无关。 所以你可以 return 一个字符串,变量或其他东西但是你必须使用 .then 来访问数据
您发出提取请求的文件
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)
});