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
00:00
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;
Log in or sign up to run or submit your code.Log inSign up
Expected Output
Clicking the button changes the text color to red.