Menu

What is Event Handling in React

Event Handling in React

To respond to user actions like button clicks, we use event handlers.

For example, when the user selects an answer:

const handleAnswerSelect = (selectedOption) => {
  if (selectedOption === correctAnswer) {
    setScore(score + 1);
    setFeedback("Correct");
  } else {
    setFeedback("Incorrect");
  }

  setTimeout(() => {
    setCurrentQuestionIndex(currentQuestionIndex + 1);
  }, 1000);
};

This function checks if the selected option is correct, updates the score, shows feedback, and then moves to the next question after a short delay.

When the user selects a category and starts the quiz from the welcome screen:

const confirmCategory = () => {
  if (category) {
    setStep("quiz");
  }
};

These event handlers make the app dynamic and interactive.