项目作者: alexanderkhivrych

项目描述 :
react use modal hook
高级语言: JavaScript
项目地址: git://github.com/alexanderkhivrych/use-modal-hook.git
创建时间: 2019-04-07T20:04:00Z
项目社区:https://github.com/alexanderkhivrych/use-modal-hook

开源协议:

下载






use-modal-hook ❤️









PRs welcome


npm package


npm downloads


Maintenance


React hook for controlling modal components

Install

  1. #With npm
  2. npm install use-modal-hook --save
  1. #With yarn
  2. yarn add use-modal-hook

Usage

Edit react use modal hook example

  1. import React, { memo } from "react";
  2. import { useModal, ModalProvider } from "use-modal-hook";
  3. import Modal from "react-modal"; // It can be any modal
  4. const MyModal = memo(
  5. ({ isOpen, onClose, title, description, closeBtnLabel }) => (
  6. <Modal isOpen={isOpen} onRequestClose={onClose}>
  7. <h2>{title}</h2>
  8. <div>{description}</div>
  9. <button onClick={onClose}>{closeBtnLabel}</button>
  10. </Modal>
  11. )
  12. );
  13. const SomePage = memo(() => {
  14. const [showModal, hideModal] = useModal(MyModal, {
  15. title: "My Test Modal",
  16. description: "I Like React Hooks",
  17. closeBtnLabel: "Close"
  18. });
  19. return (
  20. <>
  21. <h1>Test Page</h1>
  22. <button onClick={showModal}>Show Modal</button>
  23. </>
  24. );
  25. });
  26. const App = () => (
  27. <ModalProvider>
  28. <SomePage ></SomePage>
  29. </ModalProvider>
  30. );

useModal(<ModalComponent: Function|>, <modalProps: Object>, <onClose: Function>): [showModal: Function, hideModal: Function]

Param Type Description
ModalComponent Function It can be any react component that you want to use for show modal
modalProps Object Props that you want to pass to your modal component
showModal Function It is function for show your modal, you can pass any dynamic props to this function
hideModal Function It is function for hide your modal, you can pass any dynamic props to this function
onClose Function It callback will be triggered after modal window closes

showModal(dynamicModalProps: Object)

Param Type Description
dynamicModalProps Object Dynamic props that you want to pass to your modal component

License

MIT © alexanderkhivrych