Modal对话框

模态对话框。

何时使用

需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。

另外当需要一个简洁的确认框询问用户时,可以使用 Modal.confirm() 等语法糖方法。

代码演示

Modal对话框 - 图1

基本

第一个对话框。

  1. import React, { useState } from 'react';
  2. import { Modal, Button } from 'antd';
  3. const App = () => {
  4. const [isModalVisible, setIsModalVisible] = useState(false);
  5. const showModal = () => {
  6. setIsModalVisible(true);
  7. };
  8. const handleOk = () => {
  9. setIsModalVisible(false);
  10. };
  11. const handleCancel = () => {
  12. setIsModalVisible(false);
  13. };
  14. return (
  15. <>
  16. <Button type="primary" onClick={showModal}>
  17. Open Modal
  18. </Button>
  19. <Modal title="Basic Modal" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
  20. <p>Some contents...</p>
  21. <p>Some contents...</p>
  22. <p>Some contents...</p>
  23. </Modal>
  24. </>
  25. );
  26. };
  27. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图2

自定义页脚

更复杂的例子,自定义了页脚的按钮,点击提交后进入 loading 状态,完成后关闭。

不需要默认确定取消按钮时,你可以把 footer 设为 null

  1. import { Modal, Button } from 'antd';
  2. class App extends React.Component {
  3. state = {
  4. loading: false,
  5. visible: false,
  6. };
  7. showModal = () => {
  8. this.setState({
  9. visible: true,
  10. });
  11. };
  12. handleOk = () => {
  13. this.setState({ loading: true });
  14. setTimeout(() => {
  15. this.setState({ loading: false, visible: false });
  16. }, 3000);
  17. };
  18. handleCancel = () => {
  19. this.setState({ visible: false });
  20. };
  21. render() {
  22. const { visible, loading } = this.state;
  23. return (
  24. <>
  25. <Button type="primary" onClick={this.showModal}>
  26. Open Modal with customized footer
  27. </Button>
  28. <Modal
  29. visible={visible}
  30. title="Title"
  31. onOk={this.handleOk}
  32. onCancel={this.handleCancel}
  33. footer={[
  34. <Button key="back" onClick={this.handleCancel}>
  35. Return
  36. </Button>,
  37. <Button key="submit" type="primary" loading={loading} onClick={this.handleOk}>
  38. Submit
  39. </Button>,
  40. <Button
  41. key="link"
  42. href="https://google.com"
  43. type="primary"
  44. loading={loading}
  45. onClick={this.handleOk}
  46. >
  47. Search on Google
  48. </Button>,
  49. ]}
  50. >
  51. <p>Some contents...</p>
  52. <p>Some contents...</p>
  53. <p>Some contents...</p>
  54. <p>Some contents...</p>
  55. <p>Some contents...</p>
  56. </Modal>
  57. </>
  58. );
  59. }
  60. }
  61. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图3

信息提示

各种类型的信息提示,只提供一个按钮用于关闭。

  1. import { Modal, Button, Space } from 'antd';
  2. function info() {
  3. Modal.info({
  4. title: 'This is a notification message',
  5. content: (
  6. <div>
  7. <p>some messages...some messages...</p>
  8. <p>some messages...some messages...</p>
  9. </div>
  10. ),
  11. onOk() {},
  12. });
  13. }
  14. function success() {
  15. Modal.success({
  16. content: 'some messages...some messages...',
  17. });
  18. }
  19. function error() {
  20. Modal.error({
  21. title: 'This is an error message',
  22. content: 'some messages...some messages...',
  23. });
  24. }
  25. function warning() {
  26. Modal.warning({
  27. title: 'This is a warning message',
  28. content: 'some messages...some messages...',
  29. });
  30. }
  31. ReactDOM.render(
  32. <Space>
  33. <Button onClick={info}>Info</Button>
  34. <Button onClick={success}>Success</Button>
  35. <Button onClick={error}>Error</Button>
  36. <Button onClick={warning}>Warning</Button>
  37. </Space>,
  38. mountNode,
  39. );

Modal对话框 - 图4

手动更新和移除

手动更新和关闭 Modal.method 方式创建的对话框。

  1. import { Modal, Button } from 'antd';
  2. function countDown() {
  3. let secondsToGo = 5;
  4. const modal = Modal.success({
  5. title: 'This is a notification message',
  6. content: `This modal will be destroyed after ${secondsToGo} second.`,
  7. });
  8. const timer = setInterval(() => {
  9. secondsToGo -= 1;
  10. modal.update({
  11. content: `This modal will be destroyed after ${secondsToGo} second.`,
  12. });
  13. }, 1000);
  14. setTimeout(() => {
  15. clearInterval(timer);
  16. modal.destroy();
  17. }, secondsToGo * 1000);
  18. }
  19. ReactDOM.render(<Button onClick={countDown}>Open modal to close in 5s</Button>, mountNode);

Modal对话框 - 图5

销毁确认对话框

使用 Modal.destroyAll() 可以销毁弹出的确认窗。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题。

  1. import { Modal, Button } from 'antd';
  2. import { ExclamationCircleOutlined } from '@ant-design/icons';
  3. function destroyAll() {
  4. Modal.destroyAll();
  5. }
  6. const { confirm } = Modal;
  7. function showConfirm() {
  8. for (let i = 0; i < 3; i += 1) {
  9. setTimeout(() => {
  10. confirm({
  11. icon: <ExclamationCircleOutlined />,
  12. content: <Button onClick={destroyAll}>Click to destroy all</Button>,
  13. onOk() {
  14. console.log('OK');
  15. },
  16. onCancel() {
  17. console.log('Cancel');
  18. },
  19. });
  20. }, i * 500);
  21. }
  22. }
  23. ReactDOM.render(<Button onClick={showConfirm}>Confirm</Button>, mountNode);

Modal对话框 - 图6

使用 hooks 获得上下文

通过 Modal.useModal 创建支持读取 context 的 contextHolder

  1. import { Modal, Button, Space } from 'antd';
  2. const ReachableContext = React.createContext();
  3. const UnreachableContext = React.createContext();
  4. const config = {
  5. title: 'Use Hook!',
  6. content: (
  7. <>
  8. <ReachableContext.Consumer>{name => `Reachable: ${name}!`}</ReachableContext.Consumer>
  9. <br />
  10. <UnreachableContext.Consumer>{name => `Unreachable: ${name}!`}</UnreachableContext.Consumer>
  11. </>
  12. ),
  13. };
  14. const App = () => {
  15. const [modal, contextHolder] = Modal.useModal();
  16. return (
  17. <ReachableContext.Provider value="Light">
  18. <Space>
  19. <Button
  20. onClick={() => {
  21. modal.confirm(config);
  22. }}
  23. >
  24. Confirm
  25. </Button>
  26. <Button
  27. onClick={() => {
  28. modal.warning(config);
  29. }}
  30. >
  31. Warning
  32. </Button>
  33. <Button
  34. onClick={() => {
  35. modal.info(config);
  36. }}
  37. >
  38. Info
  39. </Button>
  40. <Button
  41. onClick={() => {
  42. modal.error(config);
  43. }}
  44. >
  45. Error
  46. </Button>
  47. </Space>
  48. {/* `contextHolder` should always under the context you want to access */}
  49. {contextHolder}
  50. {/* Can not access this context since `contextHolder` is not in it */}
  51. <UnreachableContext.Provider value="Bamboo" />
  52. </ReachableContext.Provider>
  53. );
  54. };
  55. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图7

自定义模态的宽度

使用 width 来设置模态对话框的宽度。

  1. import React, { useState } from 'react';
  2. import { Modal, Button } from 'antd';
  3. const App = () => {
  4. const [visible, setVisible] = useState(false);
  5. return (
  6. <>
  7. <Button type="primary" onClick={() => setVisible(true)}>
  8. Open Modal of 1000px width
  9. </Button>
  10. <Modal
  11. title="Modal 1000px width"
  12. centered
  13. visible={visible}
  14. onOk={() => setVisible(false)}
  15. onCancel={() => setVisible(false)}
  16. width={1000}
  17. >
  18. <p>some contents...</p>
  19. <p>some contents...</p>
  20. <p>some contents...</p>
  21. </Modal>
  22. </>
  23. );
  24. };
  25. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图8

异步关闭

点击确定后异步关闭对话框,例如提交表单。

  1. import { Modal, Button } from 'antd';
  2. const App = () => {
  3. const [visible, setVisible] = React.useState(false);
  4. const [confirmLoading, setConfirmLoading] = React.useState(false);
  5. const [modalText, setModalText] = React.useState('Content of the modal');
  6. const showModal = () => {
  7. setVisible(true);
  8. };
  9. const handleOk = () => {
  10. setModalText('The modal will be closed after two seconds');
  11. setConfirmLoading(true);
  12. setTimeout(() => {
  13. setVisible(false);
  14. setConfirmLoading(false);
  15. }, 2000);
  16. };
  17. const handleCancel = () => {
  18. console.log('Clicked cancel button');
  19. setVisible(false);
  20. };
  21. return (
  22. <>
  23. <Button type="primary" onClick={showModal}>
  24. Open Modal with async logic
  25. </Button>
  26. <Modal
  27. title="Title"
  28. visible={visible}
  29. onOk={handleOk}
  30. confirmLoading={confirmLoading}
  31. onCancel={handleCancel}
  32. >
  33. <p>{modalText}</p>
  34. </Modal>
  35. </>
  36. );
  37. };
  38. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图9

确认对话框

使用 confirm() 可以快捷地弹出确认框。onCancel/onOk 返回 promise 可以延迟关闭。

  1. import { Modal, Button, Space } from 'antd';
  2. import { ExclamationCircleOutlined } from '@ant-design/icons';
  3. const { confirm } = Modal;
  4. function showConfirm() {
  5. confirm({
  6. title: 'Do you Want to delete these items?',
  7. icon: <ExclamationCircleOutlined />,
  8. content: 'Some descriptions',
  9. onOk() {
  10. console.log('OK');
  11. },
  12. onCancel() {
  13. console.log('Cancel');
  14. },
  15. });
  16. }
  17. function showPromiseConfirm() {
  18. confirm({
  19. title: 'Do you want to delete these items?',
  20. icon: <ExclamationCircleOutlined />,
  21. content: 'When clicked the OK button, this dialog will be closed after 1 second',
  22. onOk() {
  23. return new Promise((resolve, reject) => {
  24. setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
  25. }).catch(() => console.log('Oops errors!'));
  26. },
  27. onCancel() {},
  28. });
  29. }
  30. function showDeleteConfirm() {
  31. confirm({
  32. title: 'Are you sure delete this task?',
  33. icon: <ExclamationCircleOutlined />,
  34. content: 'Some descriptions',
  35. okText: 'Yes',
  36. okType: 'danger',
  37. cancelText: 'No',
  38. onOk() {
  39. console.log('OK');
  40. },
  41. onCancel() {
  42. console.log('Cancel');
  43. },
  44. });
  45. }
  46. function showPropsConfirm() {
  47. confirm({
  48. title: 'Are you sure delete this task?',
  49. icon: <ExclamationCircleOutlined />,
  50. content: 'Some descriptions',
  51. okText: 'Yes',
  52. okType: 'danger',
  53. okButtonProps: {
  54. disabled: true,
  55. },
  56. cancelText: 'No',
  57. onOk() {
  58. console.log('OK');
  59. },
  60. onCancel() {
  61. console.log('Cancel');
  62. },
  63. });
  64. }
  65. ReactDOM.render(
  66. <Space>
  67. <Button onClick={showConfirm}>Confirm</Button>
  68. <Button onClick={showPromiseConfirm}>With promise</Button>
  69. <Button onClick={showDeleteConfirm} type="dashed">
  70. Delete
  71. </Button>
  72. <Button onClick={showPropsConfirm} type="dashed">
  73. With extra props
  74. </Button>
  75. </Space>,
  76. mountNode,
  77. );

Modal对话框 - 图10

国际化

设置 okTextcancelText 以自定义按钮文字。

  1. import { Modal, Button, Space } from 'antd';
  2. import { ExclamationCircleOutlined } from '@ant-design/icons';
  3. class LocalizedModal extends React.Component {
  4. state = { visible: false };
  5. showModal = () => {
  6. this.setState({
  7. visible: true,
  8. });
  9. };
  10. hideModal = () => {
  11. this.setState({
  12. visible: false,
  13. });
  14. };
  15. render() {
  16. return (
  17. <>
  18. <Button type="primary" onClick={this.showModal}>
  19. Modal
  20. </Button>
  21. <Modal
  22. title="Modal"
  23. visible={this.state.visible}
  24. onOk={this.hideModal}
  25. onCancel={this.hideModal}
  26. okText="确认"
  27. cancelText="取消"
  28. >
  29. <p>Bla bla ...</p>
  30. <p>Bla bla ...</p>
  31. <p>Bla bla ...</p>
  32. </Modal>
  33. </>
  34. );
  35. }
  36. }
  37. function confirm() {
  38. Modal.confirm({
  39. title: 'Confirm',
  40. icon: <ExclamationCircleOutlined />,
  41. content: 'Bla bla ...',
  42. okText: '确认',
  43. cancelText: '取消',
  44. });
  45. }
  46. ReactDOM.render(
  47. <Space>
  48. <LocalizedModal />
  49. <Button onClick={confirm}>Confirm</Button>
  50. </Space>,
  51. mountNode,
  52. );

Modal对话框 - 图11

自定义位置

使用 centered 或类似 style.top 的样式来设置对话框位置。

  1. import { Modal, Button } from 'antd';
  2. class App extends React.Component {
  3. state = {
  4. modal1Visible: false,
  5. modal2Visible: false,
  6. };
  7. setModal1Visible(modal1Visible) {
  8. this.setState({ modal1Visible });
  9. }
  10. setModal2Visible(modal2Visible) {
  11. this.setState({ modal2Visible });
  12. }
  13. render() {
  14. return (
  15. <>
  16. <Button type="primary" onClick={() => this.setModal1Visible(true)}>
  17. Display a modal dialog at 20px to Top
  18. </Button>
  19. <Modal
  20. title="20px to Top"
  21. style={{ top: 20 }}
  22. visible={this.state.modal1Visible}
  23. onOk={() => this.setModal1Visible(false)}
  24. onCancel={() => this.setModal1Visible(false)}
  25. >
  26. <p>some contents...</p>
  27. <p>some contents...</p>
  28. <p>some contents...</p>
  29. </Modal>
  30. <br />
  31. <br />
  32. <Button type="primary" onClick={() => this.setModal2Visible(true)}>
  33. Vertically centered modal dialog
  34. </Button>
  35. <Modal
  36. title="Vertically centered modal dialog"
  37. centered
  38. visible={this.state.modal2Visible}
  39. onOk={() => this.setModal2Visible(false)}
  40. onCancel={() => this.setModal2Visible(false)}
  41. >
  42. <p>some contents...</p>
  43. <p>some contents...</p>
  44. <p>some contents...</p>
  45. </Modal>
  46. </>
  47. );
  48. }
  49. }
  50. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图12

自定义页脚按钮属性

传入 okButtonPropscancelButtonProps 可分别自定义确定按钮和取消按钮的 props。

  1. import { Modal, Button } from 'antd';
  2. class App extends React.Component {
  3. state = { visible: false };
  4. showModal = () => {
  5. this.setState({
  6. visible: true,
  7. });
  8. };
  9. handleOk = e => {
  10. console.log(e);
  11. this.setState({
  12. visible: false,
  13. });
  14. };
  15. handleCancel = e => {
  16. console.log(e);
  17. this.setState({
  18. visible: false,
  19. });
  20. };
  21. render() {
  22. return (
  23. <>
  24. <Button type="primary" onClick={this.showModal}>
  25. Open Modal with customized button props
  26. </Button>
  27. <Modal
  28. title="Basic Modal"
  29. visible={this.state.visible}
  30. onOk={this.handleOk}
  31. onCancel={this.handleCancel}
  32. okButtonProps={{ disabled: true }}
  33. cancelButtonProps={{ disabled: true }}
  34. >
  35. <p>Some contents...</p>
  36. <p>Some contents...</p>
  37. <p>Some contents...</p>
  38. </Modal>
  39. </>
  40. );
  41. }
  42. }
  43. ReactDOM.render(<App />, mountNode);

Modal对话框 - 图13

自定义渲染对话框

自定义渲染对话框, 可通过 react-draggable 来实现拖拽。

  1. import { Modal, Button } from 'antd';
  2. import Draggable from 'react-draggable';
  3. class App extends React.Component {
  4. state = {
  5. visible: false,
  6. disabled: true,
  7. bounds: { left: 0, top: 0, bottom: 0, right: 0 },
  8. };
  9. draggleRef = React.createRef();
  10. showModal = () => {
  11. this.setState({
  12. visible: true,
  13. });
  14. };
  15. handleOk = e => {
  16. console.log(e);
  17. this.setState({
  18. visible: false,
  19. });
  20. };
  21. handleCancel = e => {
  22. console.log(e);
  23. this.setState({
  24. visible: false,
  25. });
  26. };
  27. onStart = (event, uiData) => {
  28. const { clientWidth, clientHeight } = window?.document?.documentElement;
  29. const targetRect = this.draggleRef?.current?.getBoundingClientRect();
  30. this.setState({
  31. bounds: {
  32. left: -targetRect?.left + uiData?.x,
  33. right: clientWidth - (targetRect?.right - uiData?.x),
  34. top: -targetRect?.top + uiData?.y,
  35. bottom: clientHeight - (targetRect?.bottom - uiData?.y),
  36. },
  37. });
  38. };
  39. render() {
  40. const { bounds, disabled, visible } = this.state;
  41. return (
  42. <>
  43. <Button onClick={this.showModal}>Open Draggable Modal</Button>
  44. <Modal
  45. title={
  46. <div
  47. style={{
  48. width: '100%',
  49. cursor: 'move',
  50. }}
  51. onMouseOver={() => {
  52. if (disabled) {
  53. this.setState({
  54. disabled: false,
  55. });
  56. }
  57. }}
  58. onMouseOut={() => {
  59. this.setState({
  60. disabled: true,
  61. });
  62. }}
  63. // fix eslintjsx-a11y/mouse-events-have-key-events
  64. // https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/mouse-events-have-key-events.md
  65. onFocus={() => {}}
  66. onBlur={() => {}}
  67. // end
  68. >
  69. Draggable Modal
  70. </div>
  71. }
  72. visible={visible}
  73. onOk={this.handleOk}
  74. onCancel={this.handleCancel}
  75. modalRender={modal => (
  76. <Draggable
  77. disabled={disabled}
  78. bounds={bounds}
  79. onStart={(event, uiData) => this.onStart(event, uiData)}
  80. >
  81. <div ref={this.draggleRef}>{modal}</div>
  82. </Draggable>
  83. )}
  84. >
  85. <p>
  86. Just don&apos;t learn physics at school and your life will be full of magic and
  87. miracles.
  88. </p>
  89. <br />
  90. <p>Day before yesterday I saw a rabbit, and yesterday a deer, and today, you.</p>
  91. </Modal>
  92. </>
  93. );
  94. }
  95. }
  96. ReactDOM.render(<App />, mountNode);

API

参数说明类型默认值版本
afterCloseModal 完全关闭后的回调function-
bodyStyleModal body 样式CSSProperties
cancelButtonPropscancel 按钮 propsButtonProps-
cancelText取消按钮文字ReactNode取消
centered垂直居中展示 Modalbooleanfalse
closable是否显示右上角的关闭按钮booleantrue
closeIcon自定义关闭图标ReactNode<CloseOutlined />
confirmLoading确定按钮 loadingbooleanfalse
destroyOnClose关闭时销毁 Modal 里的子元素booleanfalse
focusTriggerAfterClose对话框关闭后是否需要聚焦触发元素booleantrue4.9.0
footer底部内容,当不需要默认底部按钮时,可以设为 footer={null}ReactNode(确定取消按钮)
forceRender强制渲染 Modalbooleanfalse
getContainer指定 Modal 挂载的 HTML 节点, false 为挂载在当前 domHTMLElement | () => HTMLElement | Selectors | falsedocument.body
keyboard是否支持键盘 esc 关闭booleantrue
mask是否展示遮罩booleantrue
maskClosable点击蒙层是否允许关闭booleantrue
maskStyle遮罩样式CSSProperties
modalRender自定义渲染对话框(node: ReactNode) => ReactNode-4.7.0
okButtonPropsok 按钮 propsButtonProps-
okText确认按钮文字ReactNode确定
okType确认按钮类型stringprimary
style可用于设置浮层的样式,调整浮层位置等CSSProperties-
title标题ReactNode-
visible对话框是否可见boolean-
width宽度string | number520
wrapClassName对话框外层容器的类名string-
zIndex设置 Modal 的 z-indexnumber1000
onCancel点击遮罩层或右上角叉或取消按钮的回调function(e)-
onOk点击确定回调function(e)-

注意

  • <Modal /> 默认关闭后状态不会自动清空, 如果希望每次打开都是新内容,请设置 destroyOnClose

  • <Modal /> 和 Form 一起配合使用时,设置 destroyOnClose 也不会在 Modal 关闭时销毁表单字段数据,需要设置 <Form preserve={false} />

  • Modal.method() RTL 模式仅支持 hooks 用法。

Modal.method()

包括:

  • Modal.info

  • Modal.success

  • Modal.error

  • Modal.warning

  • Modal.confirm

以上均为一个函数,参数为 object,具体属性如下:

参数说明类型默认值版本
afterCloseModal 完全关闭后的回调function-4.9.0
autoFocusButton指定自动获得焦点的按钮null | ok | cancelok
bodyStyleModal body 样式CSSProperties4.8.0
cancelButtonPropscancel 按钮 propsButtonProps-
cancelText设置 Modal.confirm 取消按钮文字string取消
centered垂直居中展示 Modalbooleanfalse
className容器类名string-
closable是否显示右上角的关闭按钮booleanfalse4.9.0
closeIcon自定义关闭图标ReactNodeundefined4.9.0
content内容ReactNode-
getContainer指定 Modal 挂载的 HTML 节点, false 为挂载在当前 domHTMLElement | () => HTMLElement | Selectors | falsedocument.body
icon自定义图标ReactNode<QuestionCircle />3.12.0
keyboard是否支持键盘 esc 关闭booleantrue
mask是否展示遮罩booleantrue
maskClosable点击蒙层是否允许关闭booleanfalse
maskStyle遮罩样式object{}
okButtonPropsok 按钮 propsButtonProps-
okText确认按钮文字string确定
okType确认按钮类型stringprimary
style可用于设置浮层的样式,调整浮层位置等CSSProperties-
title标题ReactNode-
width宽度string | number416
zIndex设置 Modal 的 z-indexnumber1000
onCancel取消回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function(close)-
onOk点击确定回调,参数为关闭函数,返回 promise 时 resolve 后自动关闭function(close)-

以上函数调用后,会返回一个引用,可以通过该引用更新和关闭弹窗。

  1. const modal = Modal.info();
  2. modal.update({
  3. title: '修改的标题',
  4. content: '修改的内容',
  5. });
  6. // 在 4.8.0 或更高版本中,可以通过传入函数的方式更新弹窗
  7. modal.update(prevConfig => ({
  8. ...prevConfig,
  9. title: `${prevConfig.title}(新)`,
  10. }));
  11. modal.destroy();
  • Modal.destroyAll

使用 Modal.destroyAll() 可以销毁弹出的确认窗(即上述的 Modal.infoModal.successModal.errorModal.warningModal.confirm)。通常用于路由监听当中,处理路由前进、后退不能销毁确认对话框的问题,而不用各处去使用实例的返回值进行关闭(modal.destroy() 适用于主动关闭,而不是路由这样被动关闭)

  1. import { browserHistory } from 'react-router';
  2. // router change
  3. browserHistory.listen(() => {
  4. Modal.destroyAll();
  5. });

Modal.useModal()

当你需要使用 Context 时,可以通过 Modal.useModal 创建一个 contextHolder 插入子节点中。通过 hooks 创建的临时 Modal 将会得到 contextHolder 所在位置的所有上下文。创建的 modal 对象拥有与 Modal.method) 相同的创建通知方法。

  1. const [modal, contextHolder] = Modal.useModal();
  2. React.useEffect(() => {
  3. modal.confirm({
  4. // ...
  5. });
  6. }, []);
  7. return <div>{contextHolder}</div>;

FAQ

为什么 Modal 方法不能获取 context、redux、的内容和 ConfigProvider locale/prefixCls 配置?

直接调用 Modal 方法,antd 会通过 ReactDOM.render 动态创建新的 React 实体。其 context 与当前代码所在 context 并不相同,因而无法获取 context 信息。

当你需要 context 信息(例如 ConfigProvider 配置的内容)时,可以通过 Modal.useModal 方法会返回 modal 实体以及 contextHolder 节点。将其插入到你需要获取 context 位置即可:

  1. const [modal, contextHolder] = Modal.useModal();
  2. return (
  3. <Context1.Provider value="Ant">
  4. {/* contextHolder 在 Context1 内,它可以获得 Context1 的 context */}
  5. {contextHolder}
  6. <Context2.Provider value="Design">
  7. {/* contextHolder 在 Context2 外,因而不会获得 Context2 的 context */}
  8. </Context2.Provider>
  9. </Context1.Provider>
  10. );

异同:通过 hooks 创建的 contextHolder 必须插入到子元素节点中才会生效,当你不需要上下文信息时请直接调用。

如何关闭 Modal 动画?

你可以通过 transitionName=""maskTransitionName="" 去除动画 CSS,但是需要注意的是。该方法为内部方法,我们不保证下个大版本重构时该属性会被保留。

静态方法如何设置 prefixCls ?

你可以通过 ConfigProvider.config-4.13.0+) 进行设置。