0 votes
331 views
by
Design an interactive page making use of React Hooks that displays four buttons namely, “Red”, “Blue”, “Green”, “Yellow”. On clicking any of these buttons, the code displays the message that you have selected that particular color.

1 Answer

0 votes
by (98.9k points)
 
Best answer
import React, { useState } from "react";

function App() {
  const [selectedColor, setSelectedColor] = useState("");

  const handleClick = (color) => {
    setSelectedColor(color);
  };

  return (
    <div>
      <h1>Select a color</h1>
      <div>
        <button onClick={() => handleClick("Red")}>Red</button>
        <button onClick={() => handleClick("Blue")}>Blue</button>
        <button onClick={() => handleClick("Green")}>Green</button>
        <button onClick={() => handleClick("Yellow")}>Yellow</button>
      </div>
      <p>You have selected the color {selectedColor}.</p>
    </div>
  );
}

export default App;

Related questions

0 votes
0 answers 311 views
0 votes
0 answers 256 views
0 votes
0 answers 243 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

Recent Badges

Doubtly Earned a badge
Ritesh Maurya Earned a badge
Doubtly Earned a badge

5.7k questions

5.1k answers

108 comments

650 users

...