From 616d500f7c81b0dfef3db42fbe74ccd8b84d1cf1 Mon Sep 17 00:00:00 2001 From: silvertoothGeek0 Date: Fri, 29 Sep 2023 22:03:38 -0400 Subject: [PATCH] Solved --- src/App.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 96d8fe4..4dcd91b 100644 --- a/src/App.js +++ b/src/App.js @@ -1,19 +1,31 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import ResultCard from "./components/ResultCard"; import QuestionCard from "./components/QuestionCard"; import { shuffleArray } from "./lib/utils"; import rawTriviaQuestion from "./lib/data"; +const apiUrl = "https://opentdb.com/api.php?amount=1&category=9&type=multiple"; -const triviaQuestion = rawTriviaQuestion.results[0]; +let apiData +const triviaQuestion = rawTriviaQuestion.results[0]; function App() { const [selectedAnswer, setSelectedAnswer] = useState(null); - const [questionData, setQuestionData] = useState(triviaQuestion); + const [questionData, setQuestionData] = useState(triviaQuestion);//it needs to initialize with this first question const selectAnswer = (selection) => { setSelectedAnswer(selection); }; + useEffect(()=>{ + fetch(apiUrl) // fetch data whenever the state of questionData changes! + .then(raw => raw.json()) + .then(data => apiData = data.results[0]) + .catch(err => console.error(err)); + }, [questionData]); + function changeQuestion(){ + setSelectedAnswer(false); // set to false because we want to change the question and stay on question card no matter what + setQuestionData(apiData); + } let card; if (selectedAnswer) { @@ -41,7 +53,7 @@ function App() {

Trivia App

- + {card}