Цвет кнопки замены цвета в React
import React, { useState } from "react";
export default function App() {
const Colors = ["red", "green"];
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
{Colors.map((color, key) => (
<Button Color={color} Colors={Colors} key={key} />
))}
</div>
);
}
function Button({ Color, Colors }) {
const [color, setColor] = useState(Color);
const handleChange = (e) => {
e.preventDefault();
setColor(Colors[Math.floor(Math.random() * Colors.length)]);
};
const style = {
backgroundColor: color
};
return (
<button onClick={handleChange} style={style}>
Click Me!
</button>
);
}
Cloudy Cottonmouth