From e56d4fa54484a8e9cef5644e856a8aa53412204c Mon Sep 17 00:00:00 2001 From: samihazaman Date: Fri, 27 Sep 2024 23:53:03 -0400 Subject: [PATCH] Completed event handler for next question --- package-lock.json | 4 ++-- src/App.jsx | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 254efeb..bd79634 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "lab-react-trivia-solution", - "version": "0.0.0", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "lab-react-trivia-solution", - "version": "0.0.0", + "version": "0.2.0", "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1" diff --git a/src/App.jsx b/src/App.jsx index b7bde75..858674f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,14 +1,33 @@ -import { useState } from "react"; +import { 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 triviaQuestion = rawTriviaQuestion.results[0]; function App() { const [selectedAnswer, setSelectedAnswer] = useState(null); - const [questionData, setQuestionData] = useState(triviaQuestion); + const [questionData, setQuestionData] = useState(null); + + + const getNewQuestion = async () => { + try{ + const response = await fetch("https://opentdb.com/api.php?amount=1&category=9&type=multiple"); + const data = await response.json(); + setQuestionData(data.results[0]); + setSelectedAnswer(null); + + } + + catch(error){ + console.log("Error fetching question:", error); + } + }; + + useEffect(() => { + getNewQuestion(); + }, []); + + const selectAnswer = (selection) => { setSelectedAnswer(selection); @@ -16,7 +35,9 @@ function App() { let card; - if (selectedAnswer) { + + if (selectedAnswer) { + card = (

Trivia App

- + {card}