モデルズコム


デリアpor trのdesteチュートリアルは、Cera - Apobe - de o de um c ' ddio Din - Ma de Modais emがJS COM A Ajuda Do文脈に反応するということを意味します.Aqua Eestは、例示します.

コンポーネントモード


o構成要素daモーダルqueは、ポータルをします、そして、apdoをしてください.
import React, { useContext } from "react";
import ReactDOM from "react-dom";
import { ModalContext } from "../context/modal-context";
import { ModalContainer } from "./modal-styles";

interface ModalContextProps {
    isShowing: any;
    handleModal: any;
}

const Modal = ({children, alias}: any) => {
  const { isShowing } = useContext<ModalContextProps>(ModalContext); 
  let arrayModal;

  isShowing.forEach((modal: any) => {      
    if (modal.action === 'open' && alias === modal.alias) {        
        arrayModal = (<ModalContainer>{children}</ModalContainer>
      );
    }
  });  

  return isShowing.length ? ReactDOM.createPortal(arrayModal, document.body) : null;
};

export default Modal;

京大理


O P - Genの呼吸について
import React, { useContext } from 'react';
import { Container} from './home-styles';
import { ModalContext } from '../../context/modal-context';
import Modal from "../../components/modal";
import FirstPortal from "../portal/first-portal/first-portal";
import SecondPortal from '../portal/second-portal/second-portal';
import ThirdPortal from '../portal/third-portal/third-portal';

const FormDynamic: React.FC = () => {

  const { handleModal } = useContext(ModalContext);

  return (
    <Container>
       <button
          onClick={() => handleModal('open', 'first-modal')}>
            Abrir primeira modal
        </button>
       <button
          onClick={() => handleModal('open', 'second-modal')}>
            Abrir segunda modal
        </button>
       <button
          onClick={() => handleModal('open', 'third-modal')}>
            Abrir terceira modal
        </button>

        <Modal alias='first-modal'>
          <FirstPortal/>
        </Modal>
        <Modal alias='second-modal'>
          <SecondPortal/>
        </Modal>
        <Modal alias='third-modal'>
          <ThirdPortal/>
        </Modal>
  </Container>
  );
}

export default FormDynamic;

コンドミニアムDOモード


oコンテは、Ser Renderizado NAモーダルコントがm tutuo que deve ser exibido ao abrirをモーダルにします.
import React, { useContext } from 'react';
import { Portal } from '../portal-styles';
import { RiCloseLine } from "react-icons/ri";
import { ModalContext } from '../../../context/modal-context';

const FirstPortal: React.FC = () => {
  const { handleModal } = useContext(ModalContext);

  return (
  <Portal>
      <RiCloseLine onClick={() => handleModal('close', 'first-modal')}/>
      <h1>Primeira Modal</h1>
      <span>😃</span>
  </Portal>);
}

export default FirstPortal;

フック


oフックUSEMADAL LESは、楽しみのようなルベルpor armazenar os estados eを楽しみます.
import { useState } from "react";

const useModal = () => {
  let [isShowing, setIsShowing] = useState<Array<any>>([]);

  const handleModal = (action:string, alias: string) => {      
    if (action === 'close') {
        const newIsShowing =  isShowing.filter(
            modal => modal.alias !== alias
        );

        setIsShowing(newIsShowing);
    } else {
        setIsShowing([...isShowing, { action: 'open', alias }]);
    }
  };

  return {handleModal, isShowing };
};

export default useModal;

文脈


<研究ノート> Voice - Por - ser - umの中間について
import React, { ContextType, createContext, ReactChild, ReactChildren } from "react";
import useModal from "../hooks/useModal";

interface Props {
    children: ReactChild | ReactChildren;
}

export const ModalContext = createContext<any>({});

let ModalProvider = ({ children }: Props) => {

  const { handleModal, isShowing } = useModal();

  return (
    <ModalContext.Provider value={{ handleModal, isShowing }}>
      {children}
    </ModalContext.Provider>
  );
};

export default ModalProvider;