hello everyone please am trying to change the color of the {text} to red or blue
hi everyone am trying to change the color of the text to switch between blue and red color
3 Replies
const [color , setColor]= useState('red');
function handleClick(){
setColor('blue')
}
<div>
<h1
style={{ color: color}}
>My Favorite color is {color}</h1>
<button onClick={handleClick}>Change Color</button>
</div>
U code is almost correct but u need to specify the color (state) value on that element property
Also no need for 2 states ( color , text ) ,only one is enough since it's a string
U can use arrow function since it's an single line, like this
onClick={ ()=> setColor('blue') }
you forgot the
]
at the topDone ๐