DEV Community

Bellotaiwo
Bellotaiwo

Posted on

24 3

How to create a wrapper component in react.

Quick Synopsis:

In this article, you will learn about a wrapper component in react, its utility, and how to make a wrapper component.

What's a wrapper component in react?

Wrapper components are components that surround unknown components and provide a default structure to display the child components.

A wrapper component may be used to create user interface (UI) components that are used frequently throughout a design, such as modals, template pages, and information tiles.

Code example of a modal react wrapper component.



export const Modal = (props) => {
  const { visible, cancel, children } = props;
  const modalRef = useRef(null);

  useEffect(() => {
    const checkIfClickedOutside = (e) => {
      // If the modal is open and the clicked target is not within the modal, 
//then close the modal
      if (visible && modalRef.current && !modalRef.current.contains(e.target)) {
        cancel();
      }
    };

    document.addEventListener("mousedown", checkIfClickedOutside);

    // Cleanup the event listener
    return () => {
      document.removeEventListener("mousedown", checkIfClickedOutside);
    };
  }, [visible]);

  return (
    <>
      {visible && (
        <div className="modal-bg">
          <div ref={modalRef} className="modal-container">
            <button onClick={cancel}>close modal</button>
            {children}
          </div>
        </div>
      )}
    </>
  );
};


Enter fullscreen mode Exit fullscreen mode

The preceding code demonstrates a react modal component.

The modal component is given the following properties to make it reusable anywhere a modal is needed.

  • visible A Boolean value that controls the visibility of the modals.

  • cancel A handler function that negates the visibile value.

  • children displays whatever you include between the opening and closing tags when invoking the modal wrapper.

The modal component then transforms into a wrapper component, wrapping the children prop and giving it a modal view.

The modal component can be utilized anywhere a modal is required.



function App() {
  const [modalVisble, setModalVisble] = useState(false);

  const handleOpenModal = () => {
    setModalVisble(true);
  };

  const handleCloseModal = () => {
    setModalVisble(false);
  };

  return (
    <div className="App">
      <button type="button" onClick={handleOpenModal}>
        Open modal
      </button>
      <Modal visible={modalVisble} cancel={handleCloseModal}>
        <h1>Hello World</h1>
        <p>I am a modal</p>
      </Modal>
    </div>
  );
}


Enter fullscreen mode Exit fullscreen mode


.modal-bg {
  background-color: rgba(0, 0, 0, 0.2);
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-container {
  background-color: white;
  padding: 20px;
  width: 70%;
  margin: 0 auto;
}


Enter fullscreen mode Exit fullscreen mode

Image description

Conclusion

React wrapper is a must-have approach for every professional react developer.
In this article we learned how we can create a react wrapper component and its use cases.

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

Top comments (0)

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay