I have three buttons using the same click handler. - My desire is to only change the color of one button when clicked.
If another button is clicked I want the previously clicked button to go back to the color 'blue' (original state).
Example code below:
import React, { useState } from 'react';
export default function App() {
const [state, setState] = useState({
name: 'bob',
color: 'blue',
});
const handleColor = (e) => {
setState((state) => ({
...state,
color: 'red',
}));
};
return (
<div>
<button style={{ backgroundColor: state.color }} onClick={handleColor}>Click Me</button>
<button style={{ backgroundColor: state.color }} onClick={handleColor}>Click Me 2</button>
<button style={{ backgroundColor: state.color }} onClick={handleColor}>Click Me 3</button>
</div>
);
}
Are you looking something like this
Live working demo