最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Antd modal not showing on button click - Stack Overflow

programmeradmin6浏览0评论

I'm trying to use antd for my react application but the provided modal doesn't seem to work. The button is visible, but nothing happens when I click it, no error is thrown.

I also tried this with other modals provided on their official modal documentation.

Modal code:

    import { Button, Modal } from 'antd';
    import React, { useState } from 'react';
    
    const App = () => {
      const [isModalOpen, setIsModalOpen] = useState(false);
    
      const showModal = () => {
        setIsModalOpen(true);
      };
    
      const handleOk = () => {
        setIsModalOpen(false);
      };
    
      const handleCancel = () => {
        setIsModalOpen(false);
      };
    
      return (
        <>
          <Button type="primary" onClick={showModal}>
            Open Modal
          </Button>
          <Modal title="Basic Modal" open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
            <p>Some contents...</p>
            <p>Some contents...</p>
            <p>Some contents...</p>
          </Modal>
        </>
      );
    };
    
    export default App;

I'm trying to use antd for my react application but the provided modal doesn't seem to work. The button is visible, but nothing happens when I click it, no error is thrown.

I also tried this with other modals provided on their official modal documentation.

Modal code:

    import { Button, Modal } from 'antd';
    import React, { useState } from 'react';
    
    const App = () => {
      const [isModalOpen, setIsModalOpen] = useState(false);
    
      const showModal = () => {
        setIsModalOpen(true);
      };
    
      const handleOk = () => {
        setIsModalOpen(false);
      };
    
      const handleCancel = () => {
        setIsModalOpen(false);
      };
    
      return (
        <>
          <Button type="primary" onClick={showModal}>
            Open Modal
          </Button>
          <Modal title="Basic Modal" open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
            <p>Some contents...</p>
            <p>Some contents...</p>
            <p>Some contents...</p>
          </Modal>
        </>
      );
    };
    
    export default App;
Share Improve this question edited Sep 4, 2022 at 21:16 Christian 5,5214 gold badges29 silver badges44 bronze badges asked Sep 4, 2022 at 21:00 Siddharth JoshiSiddharth Joshi 3152 silver badges12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 24

Seems like Antd has updated the name of the property that is being passed to the Modal component.

The prop name open has changed to visible.

This code works:

<Modal title="Basic Modal" visible={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
    <p>Some contents...</p>
    <p>Some contents...</p>
    <p>Some contents...</p>
</Modal>

I found this by inspecting the element and altering the prop value.

Have you tried to import css correctly ?

import React, { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import 'antd/dist/antd.css';

import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);

Try importing the CSS.

import "antd/dist/antd.css"

This worked for me when I created a new app. But I faced the same issue when I tried to integrate Modal into a previous React application.

I tried changing open has changed to visible, but even that didn't let me know how you fixed it. I appreciate it.

发布评论

评论列表(0)

  1. 暂无评论