2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 4
|
2016-07-11 11:45:42 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 动态
|
|
|
|
en-US: Dynamic
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-11-18 17:05:56 +08:00
|
|
|
|
2016-07-11 11:45:42 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2015-11-18 17:05:56 +08:00
|
|
|
展示动态变化的效果。
|
|
|
|
|
2016-07-11 11:45:42 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
The count will be animated as it changes.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-10 13:00:31 +08:00
|
|
|
import React, { useState } from 'react';
|
2021-08-02 12:22:30 +08:00
|
|
|
import { Badge, Button, Switch, Divider, Avatar } from 'antd';
|
2021-01-29 14:27:36 +08:00
|
|
|
import { MinusOutlined, PlusOutlined, QuestionOutlined } from '@ant-design/icons';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2015-11-18 17:05:56 +08:00
|
|
|
const ButtonGroup = Button.Group;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2022-05-10 13:00:31 +08:00
|
|
|
const [count, setCount] = useState(5);
|
|
|
|
const [show, setShow] = useState(true);
|
2017-02-19 20:27:06 +08:00
|
|
|
|
2022-05-10 13:00:31 +08:00
|
|
|
const increase = () => {
|
|
|
|
setCount(count + 1);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-02-19 20:27:06 +08:00
|
|
|
|
2022-05-10 13:00:31 +08:00
|
|
|
const decline = () => {
|
2022-05-19 09:46:26 +08:00
|
|
|
let newCount = count - 1;
|
|
|
|
if (newCount < 0) {
|
|
|
|
newCount = 0;
|
2015-11-18 17:05:56 +08:00
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
setCount(newCount);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-02-19 20:27:06 +08:00
|
|
|
|
2022-05-10 13:00:31 +08:00
|
|
|
const random = () => {
|
2022-05-19 09:46:26 +08:00
|
|
|
const newCount = Math.floor(Math.random() * 100);
|
|
|
|
setCount(newCount);
|
2021-01-29 14:27:36 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onChange = (checked: boolean) => {
|
|
|
|
setShow(checked);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2017-02-19 20:27:06 +08:00
|
|
|
|
2022-05-10 13:00:31 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Badge count={count}>
|
|
|
|
<Avatar shape="square" size="large" />
|
|
|
|
</Badge>
|
|
|
|
<ButtonGroup>
|
|
|
|
<Button onClick={decline}>
|
|
|
|
<MinusOutlined />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={increase}>
|
|
|
|
<PlusOutlined />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={random}>
|
|
|
|
<QuestionOutlined />
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
<Divider />
|
|
|
|
<Badge dot={show}>
|
|
|
|
<Avatar shape="square" size="large" />
|
|
|
|
</Badge>
|
|
|
|
<Switch onChange={onChange} checked={show} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|