Back to Ant Design Pro

Ant Design Pro Cheatsheet

docs/cheatsheet.en-US.md

6.0.012.9 KB
Original Source

Ant Design Pro Cheatsheet

Getting Started

Create a project:

bash
git clone --depth 1 https://github.com/ant-design/ant-design-pro.git my-project
cd my-project
npm install

The project offers two modes:

  • Full mode: Includes all demo pages (Dashboard, Forms, Lists, Access, etc.), great for reference and learning
  • Simple mode: Only keeps login page and basic layout, ideal for starting from scratch

Switch to simple mode:

bash
git add -A && git commit -m "chore: save before simple"  # Commit first to allow revert
npm run simple                                             # Remove demo pages and unused deps
npm install                                                # Update dependencies

πŸ’‘ Start with full mode to learn the project structure, then switch to simple mode for development.

Directory structure:

β”œβ”€β”€ config/           # Configuration (routes, proxy, theme)
β”‚   β”œβ”€β”€ config.ts     # Main config
β”‚   β”œβ”€β”€ routes.ts     # Route definitions
β”‚   β”œβ”€β”€ defaultSettings.ts  # Layout & theme settings
β”‚   └── proxy.ts      # Dev proxy config
β”œβ”€β”€ mock/             # Mock data
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/   # Shared components
β”‚   β”œβ”€β”€ locales/      # i18n resources
β”‚   β”œβ”€β”€ models/       # Global data models
β”‚   β”œβ”€β”€ services/     # API service layer
β”‚   β”œβ”€β”€ utils/        # Utility functions
β”‚   β”œβ”€β”€ access.ts     # Permission definitions
β”‚   └── app.tsx       # Runtime configuration
β”œβ”€β”€ docs/             # Project documentation
└── types/            # Type declarations

Common commands:

CommandDescription
npm startStart dev server (UMI_ENV=dev, with Mock)
npm run devStart dev server (UMI_ENV=dev, no Mock)
npm run start:no-mockStart without Mock
npm run start:prePre-production environment
npm run start:testTest environment
npm run buildBuild for production
npm run previewPreview built output (run npm run build first, port 8000)
npm run preview:buildBuild and preview (port 8000)
npm run deployBuild and deploy to GitHub Pages
npm run analyzeAnalyze bundle size
npm run lintLint (Biome + TypeScript)
npm run biomeAuto-fix with Biome
npm testRun tests
npm run test:coverageTest with coverage
npm run test:updateUpdate test snapshots
npm run tscType check without emitting
npm run i18n-removeRemove i18n wrappers (locale=zh-CN)
npm run recordRecord request data for login scene
npm run openapiGenerate API code from OpenAPI schema
npm run simpleStrip demo pages and unused deps

πŸ’‘ UMI_ENV switches environment configs, mapping to different proxy rules in config/proxy.ts.

πŸ’‘ npm run simple removes demo pages (dashboard, form, list etc.) and unused dependencies (plots, etc.), replacing with minimal routes. Ideal for starting from scratch. Commit your code first so you can revert if needed.

Build tool: This project uses utoopack (a next-gen bundler powered by Turbopack) as the default build tool, configured via the utoopack field in config/config.ts. utoopack is Webpack-compatible and supports module.rules for custom loaders.

β†’ See umi Getting Started, utoo Docs

Routes & Menu

Route config is in config/routes.ts:

ts
export default [
  {
    path: '/welcome',
    name: 'welcome',     // maps to menu.welcome i18n key
    icon: 'home',
    component: './Welcome',
  },
  {
    path: '/admin',
    name: 'admin',
    icon: 'crown',
    access: 'canAdmin',  // route-level access control
    routes: [...],
  },
  { path: '/', redirect: '/dashboard/analysis' },
  { component: '404', path: './*' },
];

Route navigation:

tsx
import { useNavigate, useParams, useLocation } from '@umijs/max';

const navigate = useNavigate();
navigate('/dashboard');        // navigate
navigate(-1);                  // go back

const { id } = useParams();   // dynamic param /user/:id
const location = useLocation(); // current route info

Menu & access: The access field in route config controls menu visibility β€” unauthorized routes won't appear in the menu.

πŸ’‘ The name field is automatically mapped to menu.xxx i18n keys. Configure translations in src/locales/.

β†’ See umi Routes, Umi Max Layout & Menu

Layout

ProLayout config is in config/defaultSettings.ts:

ts
export default {
  navTheme: 'light',        // nav theme: light / dark
  colorPrimary: '#1890ff',  // primary color
  layout: 'mix',            // layout mode: side / top / mix
  contentWidth: 'Fluid',    // content width: Fluid / Fixed
  fixSiderbar: true,        // fixed sidebar
};

Layout modes:

  • side β€” Side navigation
  • top β€” Top navigation
  • mix β€” Top + side mixed navigation

Page container:

tsx
import { PageContainer } from '@ant-design/pro-components';

const Page = () => (
  <PageContainer
    header={{ title: 'Page Title' }}
    content="Page description"
  >
  </PageContainer>
);

Custom areas: Top-right src/components/RightContent, footer src/components/Footer.

β†’ See Umi Max Layout & Menu

Data Flow

useModel β€” lightweight global state: Create a file in src/models/ to auto-register:

ts
// src/models/counter.ts
import { useState } from 'react';

export default function useCounter() {
  const [count, setCount] = useState(0);
  const increment = () => setCount(c => c + 1);
  return { count, increment };
}
tsx
// Use in any component
import { useModel } from '@umijs/max';

const { count, increment } = useModel('counter');

useRequest β€” data fetching:

tsx
import { useRequest } from '@umijs/max';

const { data, loading, error } = useRequest(getUserInfo);

React Query β€” server state management:

tsx
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';

// Query
const { data, isLoading } = useQuery({
  queryKey: ['user', id],
  queryFn: () => getUser(id),
});

// Mutation
const mutation = useMutation({
  mutationFn: updateUser,
  onSuccess: () => {
    queryClient.invalidateQueries({ queryKey: ['user'] });
  },
});

Initial state β€” getInitialState: Define in src/app.tsx, accessible globally:

tsx
// src/app.tsx
export async function getInitialState() {
  const currentUser = await fetchUserInfo();
  return { currentUser };
}

// Use in components
import { useModel } from '@umijs/max';
const { initialState } = useModel('@@initialState');

πŸ’‘ getInitialState runs once on app startup, ideal for fetching global info (user identity, permissions).

β†’ See Umi Max Data Flow

Request

Request config is in src/app.tsx:

ts
export const request: RequestConfig = {
  baseURL: 'https://api.example.com',
  timeout: 10000,
  requestInterceptors: [],   // request interceptors
  responseInterceptors: [],  // response interceptors
};

Error handling is in src/requestErrorConfig.ts, customize error code mapping and notification logic.

Using request:

tsx
import { request } from '@umijs/max';

// GET
const data = await request('/api/users', { params: { page: 1 } });

// POST
await request('/api/users', { method: 'POST', data: { name: 'test' } });

OpenAPI code generation:

bash
npm run openapi

Auto-generates API calling code under src/services/ based on config/oneapi.json.

πŸ’‘ Generated code uses import { request } from '@umijs/max' directly β€” no manual wrapping needed.

β†’ See Umi Max Request

Access Control

Define permissions in src/access.ts:

ts
export default function access(initialState: { currentUser?: API.CurrentUser }) {
  const { currentUser } = initialState;
  return {
    canAdmin: currentUser?.access === 'admin',
    canUser: !!currentUser,
  };
}

Route-level access: Add access field in route config:

ts
{ path: '/admin', access: 'canAdmin' }

Component-level access:

tsx
import { Access, useAccess } from '@umijs/max';

// Declarative
<Access accessible={access.canAdmin}>
  <AdminPanel />
</Access>

// Imperative
const access = useAccess();
if (access.canAdmin) { /* ... */ }

β†’ See Umi Max Access

Internationalization

Config in config/config.ts:

ts
locale: {
  default: 'zh-CN',
  antd: true,         // sync antd component locale
  baseNavigator: true, // follow browser language
},

File structure:

src/locales/
β”œβ”€β”€ zh-CN.ts        # Chinese entry
β”œβ”€β”€ zh-CN/
β”‚   β”œβ”€β”€ menu.ts     # Menu translations
β”‚   β”œβ”€β”€ pages.ts    # Page translations
β”‚   └── ...
β”œβ”€β”€ en-US.ts        # English entry
└── en-US/
    └── ...

Usage:

tsx
import { useIntl, FormattedMessage } from '@umijs/max';

// Hook
const intl = useIntl();
intl.formatMessage({ id: 'menu.welcome' });

// Component
<FormattedMessage id="menu.welcome" />

Switch locale:

tsx
import { setLocale } from '@umijs/max';
setLocale('en-US', false);  // false = no page reload

β†’ See Umi Max i18n

Styling

CSS Modules: Name files *.module.less or *.module.css:

css
/* example.module.less */
.container { padding: 24px; }
.title { font-size: 16px; }
tsx
import styles from './example.module.less';
<div className={styles.container} />

antd-style (CSS-in-JS):

tsx
import { createStyles } from 'antd-style';

const useStyles = createStyles(({ token, css }) => ({
  card: css`
    background: ${token.colorBgContainer};
    border-radius: ${token.borderRadiusLG}px;
  `,
}));

const { styles } = useStyles();
<div className={styles.card} />

Tailwind CSS (v4): Use directly in className:

tsx
<div className="flex items-center gap-4 p-6 rounded-lg bg-white dark:bg-[#141414]" />

Dynamic theme: Set in config/config.ts antd config:

ts
antd: {
  configProvider: {
    theme: {
      token: {
        colorPrimary: '#1890ff',
        borderRadius: 6,
      },
    },
  },
},

Use SettingDrawer in dev mode to switch themes in real-time.

πŸ’‘ Three styling approaches can coexist: Tailwind for layout, CSS Modules for component styles, antd-style when consuming theme tokens.

β†’ See umi Styling, Umi Max antd Dynamic Theme

Testing & Debugging

Jest testing:

bash
npm test                    # Run all tests
npm run test:coverage       # With coverage report
npm run test:update         # Update snapshots

Test files go next to the component, named *.test.ts(x).

Mock data: Create files in mock/:

ts
// mock/user.ts
export default {
  'GET /api/currentUser': { name: 'Serati Ma', access: 'admin' },
  'POST /api/login': (req, res) => { res.end('ok'); },
};

Umi auto-registers mocks, active in dev mode.

Proxy config is in config/proxy.ts:

ts
export default {
  dev: {
    '/api/': {
      target: 'http://localhost:8080',
      changeOrigin: true,
    },
  },
};

πŸ’‘ Use MOCK=none to skip mock and proxy to backend: npm run start:no-mock.

β†’ See umi Testing, umi Mock

FAQ

Q: How to disable Mock? npm run start:no-mock or cross-env MOCK=none max dev

Q: How to change the primary color? Edit colorPrimary in config/defaultSettings.ts. Use SettingDrawer for live preview in dev mode.

Q: How to add a new page?

  1. Create component in src/pages/ 2. Add route in config/routes.ts 3. Add menu translation in src/locales/ (if needed)

Q: How to add global state? Create a file in src/models/ exporting a custom Hook, then use useModel('filename') in components.

Q: How to deploy? npm run build generates dist/. Deploy to any static file server. Set publicPath for non-root deployments. npm run deploy builds and publishes to GitHub Pages automatically (pushes to gh-pages branch).

Q: How to use OpenAPI code generation?

  1. Configure openAPI in config/config.ts 2. Run npm run openapi 3. Code is auto-generated under src/services/

β†’ See umi FAQ