1234567891011121314151617181920212223 |
- import {useState} from 'react';
- function Count() {
- let [count,setCount] = useState(0);
- function handleAdd() {
- setCount(count + 1);
- }
- function handleReduce() {
- setCount(count - 1);
- }
- return (
- <>
- <h1>计数器</h1>
- <h3>初始值:{count}</h3>
- <button onClick={handleAdd}>增加</button>
- <button onClick={handleReduce}>减少</button>
- <button onClick={() => {
- // 异步修改
- // setCount(count + 2)
- }}>双倍增加</button>
- </>
- )
- }
- export default Count;
|