Spin加载中

    页面局部处于等待异步数据或正在渲染过程时,合适的加载动效会有效缓解用户的焦虑。

    基本用法

    一个简单的 loading 状态。

    Spin加载中 - 图2

    1. ReactDOM.render(
    2. <div className="example">
    3. <Spin />
    4. </div>,
    5. mountNode,
    6. );

    自定义描述文案

    自定义描述文案。

    1. import { Spin, Alert } from 'antd';
    2. ReactDOM.render(
    3. <Spin tip="Loading...">
    4. <Alert
    5. message="Alert message title"
    6. description="Further details about the context of this alert."
    7. type="info"
    8. />
    9. mountNode,
    10. );

    Spin加载中 - 图4

    使用自定义指示符。

    各种大小

    小的用于文本加载,默认用于卡片容器级加载,大的用于页面级加载。

    1. import { Spin } from 'antd';
    2. ReactDOM.render(
    3. <Spin size="small" />
    4. <Spin />
    5. <Spin size="large" />
    6. </div>,
    7. mountNode,
    8. );

    可以直接把内容内嵌到 Spin 中,将现有容器变为加载状态。

    Spin加载中 - 图7

    延迟显示 loading 效果。当 spinning 状态在 delay 时间内结束,则不显示 loading 状态。

    1. import { Spin, Alert, Switch } from 'antd';
    2. class Card extends React.Component {
    3. state = { loading: false };
    4. toggle = value => {
    5. this.setState({ loading: value });
    6. };
    7. const container = (
    8. <Alert
    9. message="Alert message title"
    10. description="Further details about the context of this alert."
    11. />
    12. );
    13. return (
    14. <div>
    15. <Spin spinning={this.state.loading} delay={500}>
    16. {container}
    17. </Spin>
    18. <div style={{ marginTop: 16 }}>
    19. Loading state
    20. <Switch checked={this.state.loading} onChange={this.toggle} />
    21. </div>
    22. </div>
    23. );
    24. }
    25. }
    • Spin.setDefaultIndicator(indicator: ReactElement)同上 indicator,你可以自定义全局默认元素