visit
For example:
Component(ButtonComponent) => NewComponentWithButtonComponent Component(FormComponent) => NewComponentWithFormComponent Component(ContactComponent) => NewComponentWithContactComponent
npx create-react-app sidebarhoc
npm install react-router-dom
.header {
height: 8vh;
display: flex;
justify-content: space-between;
background-color: rgb(134, 139, 100);
align-items: center;
padding-right: 2rem;
}
.title {
font-size: 2rem;
font-weight: bold;
padding-left: 1rem;
color: #fff;
}
.logo {
font-size: 2rem;
font-weight: bold;
padding-left: 1rem;
}
.layout {
height: 100vh;
}
.sidebar {
display: flex;
flex-direction: column;
width: 20%;
height: 95vh;
background-color: rgb(209, 209, 250);
}
.nav-link {
font-size: 1.5rem;
text-decoration: none;
color: black;
text-align: center;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}
.active {
background-color: rgb(71, 71, 71);
color: white;
}
.container {
display: flex;
}
Let us define our HOC component and name it as Laoyout.jsx
Type the following command to create the Layout component.mkdir src/layouts
touch src/layouts/Layout.jsx
src/layouts/Layout.jsx
import Header from './Header';
import SideBar from './SideBar';
const Layout = (Component) => ({ ...props }) => (
<div className="layout">
<Header />
<div className="container">
<SideBar />
<div className="main-container">
<Component {...props} />
</div>
</div>
</div>
);
export default Layout;
Next, I will create the Header.jsx and SideBar.jsx inside the layouts folder by typing the following commands:
touch src/layouts/Header.jsx
touch src/layouts/SideBar.jsx
src/layouts/Header.jsx
import React from 'react'
const Header = () => {
return (
<div className="header">
<div className="logo">Logo</div>
<div className="title">
This is the header
</div>
</div>
)
}
export default Header
src/layouts/SideBar.jsx
import React from 'react'
import { NavLink } from 'react-router-dom';
const lists = [
{ name: 'Home', path: '/' },
{ name: 'About', path: '/about' },
{ name: 'Contact', path: '/contact' },
];
const SideBar = () => {
return (
<div className="sidebar">
{lists.map(list => {
return (
<NavLink to={list.path}
className={ ({isActive}) => isActive ? 'nav-link active' : 'nav-link' }
>{list.name}</NavLink>
)
})}
</div>
)
}
export default SideBar
mkdir src/routes
touch src/routes/About.jsx
touch src/routes/Contact.jsx
touch src/routes/About.jsx
import React from 'react';
const About = () => (
<div>
<h1>About page</h1>
About
this page is about all available info
</div>
);
export default About;
touch src/routes/Contact.jsx
import React from 'react';
const Contact = () => (
<div>
<h1>Contact</h1>
<h3>Phone: 0737374824</h3>
</div>
);
export default Contact;
To use our Layout HOC component we need to import it into the index.js file and pass the About and Contact components as parameters. Then configure the routes as shown below in the Index.js file
src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import App from './App';
import './index.css';
import Layout from './layouts/Layout';
import About from './routes/About';
import Contact from './routes/Contact';
// Layout component is the HOC
// we can pass App as parameters
const AppComponent = Layout(App);
// passing About component to Layout hoc as a parameter
const AboutComponent = Layout(About);
// passing Contact component to Layout hoc as a parameter
const ContactComponent = Layout(Contact)
const router = createBrowserRouter(
[
{ path: '/', element: <AppComponent /> },
{ path: '/about', element: <AboutComponent /> },
{ path: '/contact', element: <ContactComponent /> },
],
);
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>,
);
As you can see in the above code, I have used the HOC Layout where the About component is passed to produce a new component called AboutComponent. Therefore you can pass any component to produce a different result.
Lead image source.