This document will help you upgrade from antd 3.x version to antd 4.x version. If you are using 2.x or older version, please refer to the previous upgrade document to 3.x.
Upgrade preparation#
Please upgrade to the latest version of 3.x first, and remove / modify related APIs according to the console warning message.
Upgrade project React 16.12.0 or above.
If you are still using React 15, please refer to React 16 Upgrade Documentation.
For the remaining React 16 obsolete lifecycle APIs, please refer to Migration Guide.
Incompatible changes in v4#
Design specification#
Line height changes from
1.5(21px) to1.5715(22px).Darken font color from
rgba(0, 0, 0, 0.65)torgba(0, 0, 0, 0.85).4.6.0Basic rounded corner adjustment, changed from
4pxto2px.Exchanged Selected and Hovered color.
Global shadow optimization, adjusted to three layers of shadows to distinguish control hierarchies.
Icon in the bubble confirmation box has been changed from a question mark to an exclamation mark.
The color of selected components is changed to
@blue-1: #E6F7FF, and the corresponding hover color is changed to@gray-2: #FAFAFA.The color of the error was adjusted from
@red-5: #F5222Dto@red-5: #FF4D4F.The color brightness of the dividing line has been reduced from
#E8E8E8to#F0F0F0.DatePicker interactive redo, range selection can now select start and end time separately.
Table change default background color from transparent to white.
Smaller Tabs bar width.
New Tabs interaction and dom structure is changed in
4.3.0.
Compatibility#
The minimum supported version of IE is IE 11.
The minimum supported version of React is React 16.9, and some components have started to refactor using hooks.
Internal using
useMemofor performance, do not use mutable data as props.
The minimum supported version of less.js is
3.1.0, we recommend using less4.x.
Remove deprecated API#
LocaleProvider has been removed, please use
ConfigProviderinstead.Mention removed, use
Mentionsinstead.Removed the
iconTypeproperty of Alert. Please useiconinstead.Removed the
iconTypeattribute of Modal.xxx. Please useiconinstead.Removed the Form.create method,
formis now available inForm.useForm.Removed the
idattribute of Form.Item. Please usehtmlForinstead.The
setContentRefproperty of Typography has been removed, please userefinstead.Removed the
allowEmptyproperty of TimePicker, please useallowClearinstead.Removed
AfterCloseattribute of Tag, please useOnCloseinstead.Removed the
noHoveringproperty of Card, please usehoverableinstead.Removed the
verticalproperty of Carousel. Please usedotPositioninstead.Removed
wrapClassNameproperty of Drawer, please useclassNameinstead.Removed the
autosizeproperty of TextArea. Please useautoSizeinstead.Removed the
offsetattribute of Affix. Please useoffsetTopinstead.Removed the
onSearchChangeproperty of Transfer. Please useonSearchinstead.Removed the
bodyattribute of Transfer. Please usechildreninstead.Removed the
lazyattribute of Transfer, which did not really optimize the effect.Removed
comboboxmode, please useAutoCompleteinstead.Removed the
rowSelection.hideDefaultSelectionsproperty of Table, please useSELECTION_ALLandSELECTION_INVERTinrowSelection.selectionsinstead, Custom Selection.Deprecated Button.Group, please use
Spaceinstead.less variables were changed like DatePicker/TimePicker/Calendar related variables or @btn-padding-base, please check 3.x variables and 4.x variables for more details.
Icon upgrade#
In antd @ 3.9.0, we introduced the svg icon (Why use the svg icon?). The icon API using the string name cannot be loaded on demand, so the svg icon file is fully introduced, which greatly increases the size of the packaged product. In 4.0, we adjusted the icon usage API to support tree shaking, reducing the default package size of antd by about 150 KB (Gzipped).
Legacy Icon usage will be discarded:
import { Icon, Button } from 'antd';
const Demo = () => (
<div>
<Icon type="smile" />
<Button icon="smile" />
</div>
);It will be imported on demand in v4:
import { Button } from 'antd';
// tree-shaking supported
- import { Icon } from 'antd';
+ import { SmileOutlined } from '@ant-design/icons';
const Demo = () => (
<div>
- <Icon type="smile" />
+ <SmileOutlined />
<Button icon={<SmileOutlined />} />
</div>
);
// or directly import
import SmileOutlined from '@ant-design/icons/SmileOutlined';You will still be able to continue using the compatibility pack:
import { Button } from 'antd';
import { Icon } from '@ant-design/compatible';
const Demo = () => (
<div>
<Icon type="smile" />
<Button icon="smile" />
</div>
);Component refactoring#
Form rewrite.
No need to use
Form.create.Nest fields definition changes from
'xxx.yyy'to['xxx', 'yyy'].validateTriggerno long collect field value.See here for migration documentation.
DatePicker rewrite
Provide the
pickerproperty for selector switching.Range selection can now select start and end times individually.
onPanelChangewill also trigger when panel value changed.Date cell className of Custom style demo changed from
ant-calendar-datetoant-picker-cell-inner.
Tree, Select, TreeSelect, AutoComplete rewrite
use virtual scrolling.
onBlurno longer trigger value change and return React origineventobject instead.If config
validateTriggerasonBlurwith compatible Form, please change toonChangeinstead.
AutoComplete no longer support
optionLabelProp. Please set Optionvaluedirectly.AutoComplete options definition align with Select. Please use
optionsinstead ofdataSource.Select remove
dropdownMenuStyleprop.Use
listHeightto config popup height instead ofdropdownStyle.filterOptionreturn origin data with second params instead. No need to useoption.props.childrenfor matching.Tree, TreeSelect will display
labelwhentitleandlabelare both set. The adjustment is for aligning behavior withlabelInValue. New behavior (show 'label' on first node). Old behavior (show 'title' on first node).
The Grid component uses flex layout.
Button's
dangeris now treated as a property instead of a button type.Input, Select set
valuetoundefinedis uncontrolled mode now.Table rewrite.
will keep at least one column even if
columnsis empty.Nest
dataIndexdefinition changes from'xxx.yyy'to['xxx', 'yyy'].
Pagination
will default set
showSizeChangertotruesince4.1.0. This change also applied on Table component.onChangewill also trigger whenpageSizevalue changed.
Tabs rewrite. (4.3.0)
Dom structrue is changed, please check style if you override tabs css.
onPrevClick和onNextClickwould be not working anymore since we improve tabs scroll behavior.
<Table
columns={[
{
title: 'Age',
- dataIndex: 'user.age',
+ dataIndex: ['user', 'age'],
},
]}
/>Start upgrading#
You can manually check the code one by one against the above list for modification. In addition, we also provide a codemod cli tool @ant-design/codemod-v4 To help you quickly upgrade to v4.
Before running codemod cli, please submit your local code changes.
# Run directly through npx
npx -p @ant-design/codemod-v4 antd4-codemod src
# Or global installation
# Use npm
npm i -g @ant-design/codemod-v4
# Use yarn
yarn global add @ant-design/codemod-v4
# Execute
antd4-codemod srcFor parts that cannot be modified automatically, codemod will prompt on the command line, and it is recommended to modify them manually as prompted. After modification, you can run the above command repeatedly to check.
Note that codemod cannot cover all scenarios, and it is recommended to check for incompatible changes one by one.
Migration tool modification details#
@ant-design/codemod-v4 will help you migrate to antd v4. Obsolete components will be kept running through @ant-design/compatible. Generally, you don't need to migrate manually. The following sections detail the overall migration and changes.
Install compatible package#
Install @ant-design/compatible with v4-compatible-v3 tag:
npm install --save @ant-design/compatible@v4-compatible-v3Import the obsolete Form and Mention components via @ant-design/compatible package#
- import { Form, Input, Button, Mention } from 'antd';
+ import { Form, Mention } from '@ant-design/compatible';
+ import '@ant-design/compatible/assets/index.css';
+ import { Input, Button } from 'antd';
ReactDOM.render( (
<div>
<Form>
{getFieldDecorator('username')(<Input />)}
<Button>Submit</Button>
</Form>
<Mention
style={{ width: '100%' }}
onChange={onChange}
defaultValue={toContentState('@afc163')}
defaultSuggestions={['afc163', 'benjycui']}
onSelect={onSelect}
/>
</div>
);Note: Old Form imported from
@ ant-design / compatiblehas change the class name from.ant-formto.ant-legacy-form. Need to be modified accordingly if override the style.
Replace component's string icon prop with the new @ant-design/icons#
import { Avatar, Button, Result } from 'antd';
+ import { QuestionOutlined, UserOutlined } from '@ant-design/icons';
ReactDOM.render(
<div>
- <Button type="primary" shape="circle" icon="search" />
+ <Button type="primary" shape="circle" icon={SearchOutlined} />
- <Avatar shape="square" icon="user" />
+ <Avatar shape="square" icon={UserOutlined} />
<Result
- icon="question"
+ icon={<QuestionOutlined />}
title="Great, we have done all the operations!"
extra={<Button type="primary">Next</Button>}
/>
</div>,
mountNode,
);Replace v3 Icon with @ant-design/icons#
- import { Icon, Input } from 'antd';
+ import { Input } from 'antd';
+ import Icon, { CodeFilled, SmileOutlined, SmileTwoTone } from '@ant-design/icons';
const HeartSvg = () => (
<svg width="1em" height="1em" fill="currentColor" viewBox="0 0 1024 1024">
<path d="M923 plapla..." />
</svg>
);
const HeartIcon = props => <Icon component={HeartSvg} {...props} />;
ReactDOM.render(
<div>
- <Icon type="code" theme="filled" />
+ <CodeFilled />
- <Icon type="smile" theme="twoTone" twoToneColor="#eb2f96" />
+ <SmileTwoTone twoToneColor="#eb2f96" />
- <Icon type="code" theme={props.fill ? 'filled' : 'outlined'} />
+ <LegacyIcon type="code" theme={props.fill ? 'filled' : 'outlined'} />
<HeartIcon />
<Icon viewBox="0 0 24 24">
<title>Cool Home</title>
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</Icon>
<Input suffix={<SmileOutlined />} />
</div>,
mountNode,
);Replace v3 LocaleProvider with v4 ConfigProvider#
- import { LocaleProvider } from 'antd';
+ import { ConfigProvider } from 'antd';
ReactDOM.render(
- <LocaleProvider {...yourConfig}>
+ <ConfigProvider {...yourConfig}>
<Main />
- </LocaleProvider>
+ </ConfigProvider>
mountNode,
);Replace Modal.method() icon string with @ant-design/icons#
import { Modal } from 'antd';
+ import { AntDesignOutlined } from '@ant-design/icons';
Modal.confirm({
- icon: 'ant-design',
+ icon: <AntDesignOutlined />,
title: 'Do you Want to delete these items?',
content: 'Some descriptions',
onOk() {
console.log('OK');
},
onCancel() {
console.log('Cancel');
},
});Encounter problems#
v4 made a lot of detailed improvements and refactoring. We collected all known incompatible changes and related impacts as much as possible, but there may be some scenarios we have not considered. If you encounter problems during the upgrade, please go to GitHub issues and codemod Issues for feedback. We will respond and improve this document as soon as possible.