Coder Social home page Coder Social logo

nice-modal's Introduction

React 弹窗实践

一般使用弹窗的方式:

声明一个显示/隐藏的状态,再声明一个弹窗确认按钮函数,一个取消函数传给弹窗组件

const App = () => {
  const [open, setOpen] = useState(false);

  const handleOpenDialog = () => {
    setOpen(true);
  };

  const handleCloseDialog = () => {
    setOpen(false);
  };

  const handleOkDialog = data => {
    console.log(data);
    // do something
  };

  return (
    <>
      <Button variant="contained" onClick={handleOpenDialog}>
        打开Modal
      </Button>
      <MyDialog
        open={open}
        handleClose={handleCloseDialog}
        handleOk={handleOkDialog}
      />
    </>
  );
};

antd

modal.confirm({})

modal.warning({})

通过 MyModal() 这样的方式就能打开弹出,关闭弹窗的逻辑只在弹窗里面,不需要在外面维护。

如果要添加参数可以使用 MyModal({ title: "标题" }) 这样的方式。

实现

  1. 需要命令式的方式,可以使用高阶组件对弹出的组件包装一下。像这样 ModalHoc(Dialog)
  2. 实现 ModalHoc
const ModalHoc = component => {
  const ele = document.createElement("div");
  let modalRoot = null;

  const destroy = () => {
    modalRoot && modalRoot.unmount(ele);
  };

  return function (props) {
    const modalComponent = React.createElement(componet, {
      ...props,
      open: true,
      onClose: destroy,
    });

    modalRoot = ReactDOM.createRoot(ele);
    modalRoot.render(modalComponent);
  };
};
  1. 此时我们只要这样使用
const App = () => {
  const handleOpenDialog = () => {
    MyDialog({
      title: "标题111111",
      onOk: data => {
        console.log("onOk callback", data);
      },
    });
  };

  return (
    <>
      <Button variant="contained" onClick={handleOpenDialog}>
        打开Modal
      </Button>
    </>
  );
};

nice-modal's People

Contributors

iamzwq avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.