Spin

A spinner for displaying loading state of a page or a section.

When To Use#

When part of the page is waiting for asynchronous data or during a rendering process, an appropriate loading animation can effectively alleviate users' inquietude.

Examples

A simple loading status.

expand codeexpand code
import { Spin } from 'antd';
import React from 'react';

const App: React.FC = () => <Spin />;

export default App;

Spin in a container.

expand codeexpand code
import { Spin } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <div className="example">
    <Spin />
  </div>
);

export default App;
.example {
  margin: 20px 0;
  margin-bottom: 20px;
  padding: 30px 50px;
  text-align: center;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}
Loading...

Customized description content.

expand codeexpand code
import { Alert, Spin } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <Spin tip="Loading...">
    <Alert
      message="Alert message title"
      description="Further details about the context of this alert."
      type="info"
    />
  </Spin>
);

export default App;

Use custom loading indicator.

expand codeexpand code
import { LoadingOutlined } from '@ant-design/icons';
import { Spin } from 'antd';
import React from 'react';

const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;

const App: React.FC = () => <Spin indicator={antIcon} />;

export default App;

A small Spin is used for loading text, default sized Spin for loading a card-level block, and large Spin used for loading a page.

expand codeexpand code
import { Space, Spin } from 'antd';
import React from 'react';

const App: React.FC = () => (
  <Space size="middle">
    <Spin size="small" />
    <Spin />
    <Spin size="large" />
  </Space>
);

export default App;
Loading state:

Embedding content into Spin will set it into loading state.

expand codeexpand code
import { Alert, Spin, Switch } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [loading, setLoading] = useState(false);

  const toggle = (checked: boolean) => {
    setLoading(checked);
  };

  return (
    <div>
      <Spin spinning={loading}>
        <Alert
          message="Alert message title"
          description="Further details about the context of this alert."
          type="info"
        />
      </Spin>
      <div style={{ marginTop: 16 }}>
        Loading state:
        <Switch checked={loading} onChange={toggle} />
      </div>
    </div>
  );
};

export default App;
Loading state:

Specifies a delay for loading state. If spinning ends during delay, loading status won't appear.

expand codeexpand code
import { Alert, Spin, Switch } from 'antd';
import React, { useState } from 'react';

const App: React.FC = () => {
  const [loading, setLoading] = useState(false);

  const toggle = (checked: boolean) => {
    setLoading(checked);
  };
  const container = (
    <Alert
      message="Alert message title"
      description="Further details about the context of this alert."
      type="info"
    />
  );

  return (
    <div>
      <Spin spinning={loading} delay={500}>
        {container}
      </Spin>
      <div style={{ marginTop: 16 }}>
        Loading state:
        <Switch checked={loading} onChange={toggle} />
      </div>
    </div>
  );
};

export default App;

API#

PropertyDescriptionTypeDefault
delaySpecifies a delay in milliseconds for loading state (prevent flush)number (milliseconds)-
indicatorReact node of the spinning indicatorReactNode-
sizeThe size of Spin, options: small, default and largestringdefault
spinningWhether Spin is visiblebooleantrue
tipCustomize description content when Spin has childrenReactNode-
wrapperClassNameThe className of wrapper when Spin has childrenstring-

Static Method#

  • Spin.setDefaultIndicator(indicator: ReactNode)

    You can define default spin element globally.

SkeletonAnchor