Question from the React ⚛️ test

Change the style of a button using the `useState` hook in React.

Easy

What is the outcome of executing the following React code (using the useState hook) to modify the style of a button?

import React, { useState } from 'react';

function App() {
  const [isHighlighted, setIsHighlighted] = useState(false);

  const buttonStyle = isHighlighted ? { backgroundColor: 'yellow' } : {};

  return (
    <button
      style={buttonStyle}
      onMouseEnter={() => setIsHighlighted(true)}
      onMouseLeave={() => setIsHighlighted(false)}
    >
      Hover over me
    </button>
  );
}
Author: Vincent CotroStatus: PublishedQuestion passed 512 times
Edit
4
Community Evaluations
developer avatar
Mickael
07/01/2024
Un button n'a pas de background transparent par défaut, donc je ne vois pas pourquoi il redeviendrait transparent alors que nativement un il a un background: buttonface. Il faudrait plutôt préciser sa couleur d'origine si c'est la réponse désirée.
developer avatar
Auteur anonyme
23/01/2024
Complètement d'accord.
developer avatar
Auteur anonyme
07/05/2024
D'accord également