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
vic
vicโ€ข2w ago
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 top
vic
vicโ€ข2w ago
Done ๐Ÿ‘
Want results from more Discord servers?
Add your server