Skip to content

Instantly share code, notes, and snippets.

@JonahTurnquist
Last active December 15, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonahTurnquist/014bff3d5fef89c272aa1711cc10c057 to your computer and use it in GitHub Desktop.
Save JonahTurnquist/014bff3d5fef89c272aa1711cc10c057 to your computer and use it in GitHub Desktop.
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
// Layout
const Layout = ({ children }) => (
<div>
<header>My header</header>
{children}
<header>My footer</header>
</div>
);
// HOC
const withLayout = (Component) => (props) => (
<Layout>
{/* All props are passed through to the Component being wrapped */}
<Component {...props} /> /
</Layout>
);
// Pages
const PageA = withLayout(() => <p>This is Page A</p>);
const PageB = withLayout(() => <p>This is Page B</p>);
// Routes
export default function App() {
return (
<Router>
<Switch>
<Route path="/pageA" component={PageA} />
<Route path="/pageB" component={PageB} />
</Switch>
</Router>
);
}
Copy link

ghost commented Dec 15, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment