【发布时间】:2019-05-26 14:03:56
【问题描述】:
export default class App extends Component {
state = {
data: []
};
fetchData = async () => {
const response = await fetch("https://randomuser.me/api?results=5"); // Replace this with the API call to the JSON results of what you need for your app.
const json = await response.json();
this.setState({ data: json.results }); // for the randomuser json result, the format says the data is inside results section of the json.
};
所以,我在 React Native 的 App.js 文件中有这段代码。 randomuser.me 是一个只为您提供随机用户的网站。现在将其用作测试 URL。我真的不明白代码做了什么足以将它用于我项目的其他部分。我能够成功显示 5 个用户结果,但现在我想再次访问它们并遍历 state 的 data 属性。
tldr;我可以使用 data[i] 在 for 循环中访问从 fetch 中获取的数据吗?请指教。我想查看用户输入是否与存储在状态数据属性中的响应中的任何项目匹配。
【问题讨论】:
标签: arrays json react-native iterator fetch