React: 按钮点击时修改颜色

背景

当存在多个点击按钮时,需要提示用户点击的哪个按钮,所以要进行颜色的修改

import * as React from 'react';
import './style.css';

export default function App() {
  const [active, setActive] = React.useState('red');
  const handleClick = () => {
    setActive('green');
  };
  return (
    <div>
      <button style={{ backgroundColor: active }} onClick={() => handleClick()}>
        123
      </button>
    </div>
  );
}

代码截图

image

效果实现

image

posted @ 2022-11-24 13:16  胸怀丶若谷  阅读(349)  评论(0)    收藏  举报