【发布时间】:2023-03-05 11:12:02
【问题描述】:
我需要在执行 fetch 调用的 react native 中从另一个页面返回函数的结果。我使用如下方法。据我所知,这是因为异步调用。有没有一种特殊的方法可以在 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);
它将结果显示为“未定义”,但 fetch 调用有效并显示结果。有什么建议吗?
【问题讨论】:
-
您确定是
getTestval而不是getvals? -
@akond,抱歉,编辑问题时出错。我现在编辑了它。
-
我看到您正在使用 react-redux 连接,但我根本没有看到 mapStateToProps 定义。你甚至都在使用它吗?你会考虑不使用 redux 的解决方案吗?
标签: javascript react-native async-await fetch-api