[英]How to add objects retrieved from fetch request to the state array in React and then update the view?
[英]how to add an array of objects in a fetch request
你應該使用 await 或者 then 如果你想使用那么你可以這樣做
fetch('you api', {
method: 'POST',
body: JSON.stringify({
// here put data you want to send
})
})
.then((response) => response.json())
.then((json) => {
// you can do what ever you want here
console.log(json)
}
)
或者如果你想使用 async-await 使用這個
const getData = async () => {
const response = await fetch('your api', {
method: 'POST',
body: JSON.stringify({
// your data
}),
});
const data = await response.json();
console.log(data);
// do what ever you want here
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.