Count.jsx 615 B

1234567891011121314151617181920212223
  1. import {useState} from 'react';
  2. function Count() {
  3. let [count,setCount] = useState(0);
  4. function handleAdd() {
  5. setCount(count + 1);
  6. }
  7. function handleReduce() {
  8. setCount(count - 1);
  9. }
  10. return (
  11. <>
  12. <h1>计数器</h1>
  13. <h3>初始值:{count}</h3>
  14. <button onClick={handleAdd}>增加</button>
  15. <button onClick={handleReduce}>减少</button>
  16. <button onClick={() => {
  17. // 异步修改
  18. // setCount(count + 2)
  19. }}>双倍增加</button>
  20. </>
  21. )
  22. }
  23. export default Count;