--- order: 12 title: zh-CN: 使用 hooks 获得上下文 en-US: Use hooks to get context --- ## zh-CN 通过 `Modal.useModal` 创建支持读取 context 的 `contextHolder`。 ## en-US Use `Modal.useModal` to get `contextHolder` with context accessible issue. ```tsx import { Button, Modal, Space } from 'antd'; import React, { createContext } from 'react'; const ReachableContext = createContext(null); const UnreachableContext = createContext(null); const config = { title: 'Use Hook!', content: ( <> {name => `Reachable: ${name}!`}
{name => `Unreachable: ${name}!`} ), }; const App: React.FC = () => { const [modal, contextHolder] = Modal.useModal(); return ( {/* `contextHolder` should always under the context you want to access */} {contextHolder} {/* Can not access this context since `contextHolder` is not in it */} ); }; export default App; ```