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();
},[])
what coming from your data is below. I suspect what you want is setQuiz(data.results) also initializing like useState([]) should help too.
const data = {
response_code: 0,
results: [
{
category: 'History',
type: 'multiple',
difficulty: 'medium',
question: 'Joseph Smith was the founder of what religion?',
correct_answer: 'Mormonism',
incorrect_answers: ['Buddhism', 'Christianity', 'Hinduism'],
},
{
category: 'Sports',
type: 'multiple',
difficulty: 'medium',
question: 'In a game of snooker, what colour ball is worth 3 points?',
correct_answer: 'Green',
incorrect_answers: ['Yellow', 'Brown', 'Blue'],
},
],
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.