How to create reusable custom modal component in React?

后端 未结 2 1616
北荒
北荒 2021-01-24 00:41

I have a problem with the concept of modals in React. When using server side rendered templates with jQuery I was used to have one empty global modal template always available (

2条回答
  •  灰色年华
    2021-01-24 01:09

    There are a few ways of doing this. The first involves passing in the modal state from a parent component. Here's how to do this - first with the parent App.js component:

    // App.js
    
    import React from "react";
    
    import Modal from "./Modal";
    
    const App = () => {
      const [showModal, updateShowModal] = React.useState(false);
    
      const toggleModal = () => updateShowModal(state => !state);
    
      return (
        

    Not a modal

    ); } export default App;

    And here's the Modal.js child component that will render the modal:

    // Modal.js
    
    import React from "react";
    
    const modalStyles = {
      position: "fixed",
      top: 0,
      left: 0,
      width: "100vw",
      height: "100vh",
      background: "blue"
    };
    
    const Modal = ({ canShow, updateModalState }) => {
      if (canShow) {
        return (
          

    I'm a Modal!

    ); } return null; }; export default Modal;

    This way is perfectly fine, but it can get a bit repetitive if you're reusing the modal in many places throughout your app. So instead, I would recommend using the context API.

    Define a context object for your modal state, create a provider near the top of your application, then whenever you have a child component that needs to render the modal, you can render a consumer of the modal context. This way you can easily nest your modal deeper in your component tree without having to pass callbacks all the way down. Here's how to do this - first by creating a context.js file:

    // context.js
    
    import React from "react";
    
    export const ModalContext = React.createContext();
    

    Now the updated App.js file:

    // App.js
    
    import React from "react";
    
    import { ModalContext } from "./context";
    import Modal from "./Modal";
    
    const App = () => {
      const [showModal, updateShowModal] = React.useState(false);
    
      const toggleModal = () => updateShowModal(state => !state);
    
      return (
        
          

    Not a modal

    ); } export default App;

    And lastly the updated Modal.js file:

    // Modal.js
    
    import React from "react";
    
    import { ModalContext } from "./context";
    
    const modalStyles = {
      position: "fixed",
      top: 0,
      left: 0,
      width: "100vw",
      height: "100vh",
      background: "blue"
    };
    
    const Modal = () => {
      return (
        
          {context => {
            if (context.showModal) {
              return (
                

    I'm a Modal!

    ); } return null; }}
    ); }; export default Modal;

    Here's a Codesandbox link with a working version using context. I hope this helps!

提交回复
热议问题