【发布时间】:2021-11-06 23:38:42
【问题描述】:
我目前正在 useEffect 中放置一个 API GET 请求调用。这里的想法是在每次修改状态时“重新渲染”列表。
const[superheroes, setSuperheroes] = useState([]);
useEffect(() => {
apiCall('/superheroes', { method: 'GET'})
.then(response => {
response.json().then(response => {
setSuperheroes(response);
}).catch(x=> console.log("ERROR: " + x))
})
}, [superheroes])
即使superheroesstate 尚未更改,这样做也会导致无限循环调用。
我能清楚地解释为什么会这样吗?
编辑:澄清。我故意不使用空数组作为 useEffect() 的第二个参数,因为我不想运行一次。
【问题讨论】:
标签: reactjs api react-hooks infinite-loop use-effect