【发布时间】:2020-02-03 03:54:36
【问题描述】:
我有一个使用 Hooks 的反应组件,我单击一个按钮以对 Hacker News API 进行 API 调用并将结果推送到一个数组中。然后我将“故事”的状态设置为充满故事的数组。
我有第二个功能由一个按钮触发,该按钮控制台记录“故事”的状态,并且 console.log 是一个返回每个故事标题的 .map。所有这些工作都很好。
如果我尝试在组件的返回中使用 .map,它将不起作用。如果我将“故事”的状态初始化为["test", "test1", "test2"],则.map 可以工作,但是一旦我按下按钮将状态设置为故事数组,.map 就会停止工作。没有错误消息,只是内容消失了。
这里是我导入 React 并设置初始状态的地方,我使用 Axios、Fetch 和 Wretch 进行 API 调用,结果都一样:
import React, { useState, useEffect } from 'react';
const axios = require('axios');
import wretch from "wretch"
function App () {
const [stories, setStories] = useState(["test", "test2", "test3"]);
这是我触发 API 并设置状态的函数:
function call () {
let storiesArr = [];
fetch('http://hacker-news.firebaseio.com/v0/topstories.json')
.then ((res) => res.json())
.then ((data) => {
for (let i = 0; i < 20; i++) {
fetch(`http://hacker-news.firebaseio.com/v0/item/${data[i]}.json`)
.then((res) => res.json())
.then((eachStory) => {
storiesArr.push(eachStory);
})
}
})
这是我用来检查状态是否设置为我认为的状态并确保 .map 在“故事”状态下工作的第二个函数。这对我有用:
function test () {
console.log(stories);
stories.map((each) => {
return <p>{each.title}</p>
})
}
这是我的回报,这里的 .map 确实适用于初始状态,但一旦我将 state 设置为新数组就不能:
return (
<>
<h1 onClick={ () => call() } className="click">Fire off API calls</h1>
<h1 onClick={ () => test() } className="click">Test state of stories/<br/>.map each title</h1>
<table className="table">
<thead>
<tr>
<td>Table</td>
</tr>
</thead>
<tbody>
{
stories.map((each, i) => {
return <tr key={i}>
<td>{each.title ? each.title : each}</td>
</tr>
})
}
</tbody>
</table>
</>
);
我无法弄清楚为什么 .map 开始工作,不再在返回中工作,但在函数中工作....
我将非常感谢任何人可以提供的任何帮助。
【问题讨论】:
-
调用
setStories时可能有问题,能否显示调用位置? -
同意,您需要在使用 setStories 的地方显示代码。这个问题的整个前提都围绕着它,所以我不知道你为什么把它遗漏了..
-
我不敢相信我做到了。这是调用函数中的最后一件事,在函数关闭之前但在 .then 之外。对不起。我不知何故在我的复制/粘贴中错过了它。 setStories(storiesArr)
-
@henryfrank fetch 是异步的,请将 setStories 放入最后一个 .then() 中,否则会遇到异步问题
-
谢谢!这似乎是问题所在。
标签: javascript reactjs react-hooks