Wrapper Component in React
govindbisen

govindbisen @govindbisen

About: Full Stack Developer

Location:
Bharat
Joined:
Sep 8, 2021

Wrapper Component in React

Publish Date: Jan 17 '22
5 2

Hi, I would like to introduce wrapper.

What is a wrapper?

Wrapper is a react concept, it is like a fragment <></> that covers any component or jsx as a blanket.

If you will use material ui component or Ant design, most probably you will encounter wrappers.

you can also make your own, let's have a look.

Wrapper.js

import React from "react";

export default function Wrapper(props) {
  return props.children;
}
Enter fullscreen mode Exit fullscreen mode

This simple wrapper component will return everything it covers, it will not add anything on it's your own.

Now when you have build your own, you can use it anywhere in the project.

Home.js

import Wrapper from "../wrapper/Wrapper";
export function Home() {
  let navigate = useNavigate();
  return (

      <Wrapper>
        I am home component...
       <button onClick={() => navigate(`/Login`)}>go to login</button> 
      </Wrapper>

  );
}
Enter fullscreen mode Exit fullscreen mode

This is my first article, As shortest as possible, if I have done any mistake, you are welcome to give me feedback, I will improve.

Comments 2 total

  • Diego Souza
    Diego SouzaJan 17, 2022

    Why you used a Wrapper component (nice idea) inside a Fragment?

    This Wrapper it's like a Fragment, right?

    • govindbisen
      govindbisenJan 20, 2022

      yes, there is no need to use fragment. I forgot to remove it.

Add comment