Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ function App() {
setSelectedAnswer(selection);
};

const fetchNewQuestion = async () => {
try {
const response = await fetch('https://opentdb.com/api.php?amount=1&category=9&type=multiple');
if (!response.ok) {
throw new Error(`HTTP error. Status: ${response.status}`);
}
const newQuestionData = await response.json();
if (!newQuestionData.results || newQuestionData.results.length === 0) {
throw new Error('No questions found in API response.');
}
setSelectedAnswer(null);
setQuestionData(newQuestionData.results[0]);
} catch (error) {
console.error('Error fetching new question data: ', error.message);
}
};

let card;

if (selectedAnswer) {
Expand Down Expand Up @@ -41,7 +58,7 @@ function App() {
<div className="w-100 my-5 d-flex justify-content-center align-items-center">
<div style={{ maxWidth: "45%" }}>
<h1 className="text-center">Trivia App</h1>
<button className="btn btn-success">Next Question</button>
<button className="btn btn-success" onClick={fetchNewQuestion}>Next Question</button>
{card}
</div>
</div>
Expand Down
Loading