admin管理员组文章数量:1023782
I'm building a project with vite + react app.
I fetched data in useEffect()
and got the following error in console:
GET .php?amount=5&category=9&difficulty=medium&type=multiple 429 (Too Many Requests)
Here's my code :
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
fetch(".php?amount=5&category=9&difficulty=medium&type=multiple")
.then((res)=> res.json())
.then((data)=>console.log(data))
},[])
return (
<div >...</div>
)
}
I set the second argument in useEffect(function,[]), why is it fetching multiple times? How to I fix it? I'm a newbie!
I am expecting to fetch data only one time on render and update the state.
I'm building a project with vite + react app.
I fetched data in useEffect()
and got the following error in console:
GET https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple 429 (Too Many Requests)
Here's my code :
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
fetch("https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple")
.then((res)=> res.json())
.then((data)=>console.log(data))
},[])
return (
<div >...</div>
)
}
I set the second argument in useEffect(function,[]), why is it fetching multiple times? How to I fix it? I'm a newbie!
I am expecting to fetch data only one time on render and update the state.
Share Improve this question asked Nov 25, 2023 at 14:22 Samiul IslamSamiul Islam 411 silver badge7 bronze badges 4- 1 If your code was fetching multiple times it would show that in the console (which I'm guessing it isn't?) My guess is that (because it's a free open API) a lot of people are hammering it at the same which is why the API server is having an issue. It's not an error specifically for you - it's an error for everyone using the service. I don't see an issue with your code per se. – Andy Commented Nov 25, 2023 at 14:28
-
1
A
429 (Too Many Requests)
normally tells you that you hit the rate limit for the allowed request for a given time span. – t.niese Commented Nov 25, 2023 at 14:30 - yeah that could be, I fetched data the same way in another project and I got no error there. @Andy – Samiul Islam Commented Nov 25, 2023 at 14:32
-
@Andy A
429 (Too Many Requests)
is normally - if used correctly - a per user/ip/token based response. If the server can't keep up in general you more likely get a503
,504
or408
(or maybe also a500
or502
). – t.niese Commented Nov 25, 2023 at 14:41
2 Answers
Reset to default 2Let's test our API first. Open your API URL separately in a browser tab and check whether the JSON response is returned. Refresh the API multiple times and see the results are consistent. If not, the API is having some issue responding to request back to back or designed in a way to return the response after a configured wait time.
In order to prevent react app from rendering its ponents twice is to take out the StrictMode from your root ponent. The code is just fine.
Firstly, React will render your ponents twice in development mode, see this article for more on that https://react.dev/reference/react/StrictMode
Second, you're not actually setting the state to the fetched data.
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
(async () => {
const response = await fetch("https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple");
// Check status codes and whatnot here and handle accordingly
const data = await response.json();
console.log(data);
setData(data);
})();
},[])
return (
<div >...</div>
)
}
If you're still seeing multiple renders/fetches you'll have to look in the parent ponents to see if something might be triggering new renders.
I'm building a project with vite + react app.
I fetched data in useEffect()
and got the following error in console:
GET .php?amount=5&category=9&difficulty=medium&type=multiple 429 (Too Many Requests)
Here's my code :
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
fetch(".php?amount=5&category=9&difficulty=medium&type=multiple")
.then((res)=> res.json())
.then((data)=>console.log(data))
},[])
return (
<div >...</div>
)
}
I set the second argument in useEffect(function,[]), why is it fetching multiple times? How to I fix it? I'm a newbie!
I am expecting to fetch data only one time on render and update the state.
I'm building a project with vite + react app.
I fetched data in useEffect()
and got the following error in console:
GET https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple 429 (Too Many Requests)
Here's my code :
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
fetch("https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple")
.then((res)=> res.json())
.then((data)=>console.log(data))
},[])
return (
<div >...</div>
)
}
I set the second argument in useEffect(function,[]), why is it fetching multiple times? How to I fix it? I'm a newbie!
I am expecting to fetch data only one time on render and update the state.
Share Improve this question asked Nov 25, 2023 at 14:22 Samiul IslamSamiul Islam 411 silver badge7 bronze badges 4- 1 If your code was fetching multiple times it would show that in the console (which I'm guessing it isn't?) My guess is that (because it's a free open API) a lot of people are hammering it at the same which is why the API server is having an issue. It's not an error specifically for you - it's an error for everyone using the service. I don't see an issue with your code per se. – Andy Commented Nov 25, 2023 at 14:28
-
1
A
429 (Too Many Requests)
normally tells you that you hit the rate limit for the allowed request for a given time span. – t.niese Commented Nov 25, 2023 at 14:30 - yeah that could be, I fetched data the same way in another project and I got no error there. @Andy – Samiul Islam Commented Nov 25, 2023 at 14:32
-
@Andy A
429 (Too Many Requests)
is normally - if used correctly - a per user/ip/token based response. If the server can't keep up in general you more likely get a503
,504
or408
(or maybe also a500
or502
). – t.niese Commented Nov 25, 2023 at 14:41
2 Answers
Reset to default 2Let's test our API first. Open your API URL separately in a browser tab and check whether the JSON response is returned. Refresh the API multiple times and see the results are consistent. If not, the API is having some issue responding to request back to back or designed in a way to return the response after a configured wait time.
In order to prevent react app from rendering its ponents twice is to take out the StrictMode from your root ponent. The code is just fine.
Firstly, React will render your ponents twice in development mode, see this article for more on that https://react.dev/reference/react/StrictMode
Second, you're not actually setting the state to the fetched data.
import React,{useState,useEffect} from 'react'
function QuizMain() {
const[data,setData] = useState()
useEffect(()=> {
(async () => {
const response = await fetch("https://opentdb./api.php?amount=5&category=9&difficulty=medium&type=multiple");
// Check status codes and whatnot here and handle accordingly
const data = await response.json();
console.log(data);
setData(data);
})();
},[])
return (
<div >...</div>
)
}
If you're still seeing multiple renders/fetches you'll have to look in the parent ponents to see if something might be triggering new renders.
本文标签:
版权声明:本文标题:javascript - getting 429 (Too many requests) ERROR while fetching data in useState(), how do I fix it? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745588324a2157734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论