A React library for managing page layouts in a composable and efficient way. This library helps you create reusable layouts that persist across route changes, preventing unnecessary remounts of layout components.
- 🔄 Persistent layouts across route changes
- 🎯 Efficient layout management with minimal re-renders
- 🔌 Optional integration with React Router
- 📦 Lightweight with minimal dependencies (only requires React)
npm install react-compose-layout
# or
yarn add react-compose-layout- React ^18.3.1
- Node.js >=20.0.0
The library has been tested against React Router v6 but does not directly depend on it. React Router is an optional peer dependency that you can install if you need routing capabilities.
First, wrap your application with the PageLayoutProvider:
import { PageLayoutProvider } from "react-compose-layout";
function App() {
return <PageLayoutProvider>{/* Your app content */}</PageLayoutProvider>;
}Create your layout component as you normally would:
function MainLayout({ children, title }) {
return (
<div>
<header>
<nav>{/* Your navigation */}</nav>
</header>
<main>
<h1>{title}</h1>
{children}
</main>
<footer>{/* Your footer */}</footer>
</div>
);
}Use createPageLayout to create a layout component that can be used across different pages:
import { createPageLayout } from "react-compose-layout";
const Layout = createPageLayout({ Component: MainLayout });Apply the layout to your pages and pass any props it needs:
function HomePage() {
return (
<Layout title="Welcome to the Home Page">
<p>This is our homepage content</p>
{/* Your page content */}
</Layout>
);
}
function AboutPage() {
return (
<Layout title="About Us">
<p>Learn more about our company</p>
{/* Your page content */}
</Layout>
);
}For proper integration with React Router, you'll need to use the PageRenderOutlet component along with React Router's Outlet. Note that PageRenderOutlet can also be used without React Router in other scenarios where you need to control layout rendering:
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";
import { PageLayoutProvider, PageRenderOutlet } from "react-compose-layout";
function App() {
return (
<BrowserRouter>
<PageLayoutProvider>
<Routes>
<Route
element={
<PageRenderOutlet>
<Outlet />
</PageRenderOutlet>
}
>
<Route path="/" element={<HomePage />} />
<Route path="/about" element={<AboutPage />} />
</Route>
</Routes>
</PageLayoutProvider>
</BrowserRouter>
);
}The library uses React's Context API to manage layouts efficiently. When you switch between pages that use the same layout, the layout component doesn't remount, improving performance and maintaining layout state. This is achieved through the PageLayoutProvider and PageRenderOutlet components working together to manage the layout lifecycle.
A provider component that manages the layout state.
<PageLayoutProvider>{/* Your app content */}</PageLayoutProvider>A function that creates a layout component.
createPageLayout({ Component: LayoutComponent });Component: The React component to be used as a layout
- A layout component that can be used to wrap page content
MIT
Contributions are welcome! Please feel free to submit a Pull Request.