如何将获取的数据放入州?
我试图将获取的数据放入州,但它不起作用,请让我知道我在这里做错了什么,我也希望特定的数据像在此示例中一样,我只想要问题,答案和不正确答案,我也想将整个数据放入对象的数组中
const [quiz,setQuiz]=useState({});
useEffect(()=>{
const getQuizData =async ()=>{
const res = await fetch("https://opentdb.com/api.php?amount=10")
const data = await res.json();
setQuiz({data});
}
getQuizData();
},[])
I am trying to put fetched data into the state but it's not working, please let me know what I am doing wrong here, and also I want specific data to be inside the state like in this example I want only questions, answers, and incorrect answer, also I wanted to put the whole data into the array of object
const [quiz,setQuiz]=useState({});
useEffect(()=>{
const getQuizData =async ()=>{
const res = await fetch("https://opentdb.com/api.php?amount=10")
const data = await res.json();
setQuiz({data});
}
getQuizData();
},[])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自您的数据的内容如下。我怀疑您想要的是setquiz(data. recults),例如usestate([])也应该有所帮助。
what coming from your data is below. I suspect what you want is setQuiz(data.results) also initializing like useState([]) should help too.