eBay / nice-modal-react

A modal state manager for React.
https://ebay.github.io/nice-modal-react
MIT License
2.1k stars 122 forks source link

[deep loop] Can't use in component? #138

Closed rks118 closed 11 months ago

rks118 commented 11 months ago

When I first enter the page, I need a modal to alert the user if there is no data:

// RegisterModal
import {
  Box,
  Modal,
  ModalCloseButton,
  ModalContent,
  ModalHeader,
  ModalOverlay,
} from '@chakra-ui/react';
import NiceModal, { useModal } from '@ebay/nice-modal-react';
export const RegisterModal = NiceModal.create<{ name: string }>(({ name }) => {
  const modal = useModal();

  return (
    <Modal isOpen={modal.visible} onClose={modal.hide}>
      <ModalOverlay />
      <ModalContent>
        <ModalHeader>Modal Title {name}</ModalHeader>
        <ModalCloseButton />
        <button
          onClick={() => {
            modal.hide();
          }}
        >
          {' '}
          close modal{' '}
        </button>
      </ModalContent>
    </Modal>
  );
});

Usage (deep loop):

const data = useFetch('/api/getUser')
const modal = useModal(RegisterModal);

if (!data) {
  modal.show({ name: 'no data' });
}

return <div>...</div>

Usage (deep loop):

const data = useFetch('/api/getUser')
if (!data) {
  NiceModal.show(RegisterModal, { name: 'no data' });
}

return <div>...</div>
ArturKustyaev commented 11 months ago

Do this in useEffect