【发布时间】:2021-05-22 20:54:51
【问题描述】:
我正在向 API 发出 fetch 请求,请求成功,但是当我 console.log 响应时,它会被记录两次。
我的组件
const Content = () => {
let userId = JSON.parse(localStorage.getItem('user'));
fetch(`http://localhost:3000/post/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'authorization': 'Bearer ' + userId.token,
},
}).then(response => response.json())
.then(data => {
console.log(data)
}
);
return (
<Container>
<Card>
<Name></Name>
<Time></Time>
<Image></Image>
<Text></Text>
</Card>
</Container>
);
};
控制台会记录:
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
为什么我会得到两个响应?
【问题讨论】: