Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a component that changes its text color to red when a button is clicked.

Easy

Problem Statement

Create a component that changes its text color to red when a button is clicked.

Sample Output

Clicking the button changes the text color to red.

Limits

Time: 2s · Memory: 256 MB

Code Editor

Try your code in this preview. Submit marks it done.

import React, { useState } from "react";
 
 const ColorChanger = () => {
  const [color, setColor] = useState("black");
 
  const handleClick = () => {
  // Change the state to set the color to red
  };
 
  return (
  <div>
  <p style={{ color }}>This text will change color.</p>
  <button onClick={handleClick}>Change Color</button>
  </div>
  );
 };
 
 export default ColorChanger;