Updating React.js Box Component Color on Click and Managing State with Hooks

Posted by

Changing React.js Box Component Color with a Click

Changing React.js Box Component Color with a Click

In this tutorial, we will create a React.js component that changes its color when clicked. We will also keep track of the state of the component using React Hooks.

const { useState } = React;

const BoxComponent = () => {
const [color, setColor] = useState(‘gray’);

const handleClick = () => {
const newColor = color === ‘gray’ ? ‘blue’ : ‘gray’;
setColor(newColor);
};

return (

);
};

ReactDOM.render(, document.getElementById(‘box’));