admin管理员组文章数量:1317572
I want to assign response in Fruits array ones I fetched the data from Api using fetch...But I am getting empty array when console.log.. I am getting the response from Api but not able to assign it to fruits
I am doing this way: .then(data => Fruits);
let Fruits = []
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"dfdfdffd"})
};
fetch('', requestOptions)
.then(response => response.json())
.then(data => Fruits);
}, []);
I want to assign response in Fruits array ones I fetched the data from Api using fetch...But I am getting empty array when console.log.. I am getting the response from Api but not able to assign it to fruits
I am doing this way: .then(data => Fruits);
let Fruits = []
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"dfdfdffd"})
};
fetch('https://d./audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits);
}, []);
Share
Improve this question
edited Mar 18, 2022 at 9:22
James_sheford
asked Mar 17, 2022 at 12:15
James_shefordJames_sheford
3031 gold badge3 silver badges15 bronze badges
1
-
2
You most likely want to make
Fruits
state:const [fruits, setFruits] = useState([]);
, and then set your state:.then(data => setFruits(data));
. When you use state, your ponent will rerender (so then you can make your ponent use fruits to display content) – Nick Parsons Commented Mar 17, 2022 at 12:23
3 Answers
Reset to default 4Use a state
const [fruits, setFruits] = useState([]);
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d./audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data);
}, []);
You're not assigning data
to anything. Try:
fetch('https://d./audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => Fruits = data);
Also, you should use a state do store the information.
const [fruits, setFruits] = useState();
fetch('https://d./audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
Maybe use a state?
const [fruits, setFruits] = useState([])
useEffect(() => {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIwicm9sZSI6ImV4cGVydCJ9.6pYrAr_En0nl4N52oP1O7WRJA6PPFGCzUebauBIOEnc", },
body: JSON.stringify({"firebase_id":"foCzPM8MgOtg1"})
};
fetch('https://d./audis/el/lt', requestOptions)
.then(response => response.json())
.then(data => setFruits(data));
}, []);
console.log(fruits);
本文标签: javascriptHow to assign response from Api to an array using fetch functionStack Overflow
版权声明:本文标题:javascript - How to assign response from Api to an array using fetch function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742021775a2414810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论