Why getPopupContainer doesn't work correctly in my react app? – Javascript

by
Ali Hasan
antd react-typescript reactjs

Quick Fix: Wrap the div element in App component as –

<ConfigProvider
  theme={theme}
  getPopupContainer={() => modalContainerRef.current as HTMLElement}
>
  <App>
    <div ref={modalContainerRef}>{children}</div>
  </App>
</ConfigProvider>

Also, use modal.confirm from App.useApp() instead of Modal.confirm.

The Solutions:

Solution 1: Dynamic App Component

To resolve the issue with getPopupContainer, you should utilize the dynamic App component provided by Ant Design. Here’s how:

import { App } from 'antd';

const ThemeWrapper = ({ children }) => {
  const modalContainerRef = useRef<HTMLDivElement>(null);

  return (
    <ConfigProvider
      theme={theme}
      getPopupContainer={() => modalContainerRef.current as HTMLElement}
    >
      <App>
        <div ref={modalContainerRef}>{children}</div>
      </App>
    </ConfigProvider>
  );
};

In the onLogout function, replace Modal.confirm with App.useApp().modal.confirm:

const onLogout = () => {
  App.useApp().modal.confirm({
    // ...
  });
};

Q&A

Why getPopupContainer doesn’t work correctly in React App?

In React 18 concurrent mode, static function is not well supported. You need to set up your project using hooks like App.useApp() from antd

Video Explanation:

The following video, titled "How to use AntD's Datepicker with React Hook Form - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this video we integrate the Antd #datepicker component with #reactjs Hook Form in a reusable way. We end up with a reusable component ...