Skip to content

Instantly share code, notes, and snippets.

@bultas
Last active May 18, 2016 08:40
Show Gist options
  • Save bultas/54a5d58dc7df31a6465df1f4e10bf50e to your computer and use it in GitHub Desktop.
Save bultas/54a5d58dc7df31a6465df1f4e10bf50e to your computer and use it in GitHub Desktop.
Composable High-order components
export function composeComponents(component, wrappers = []) {
return wrappers.reduce((c, wrapper) => wrapper(c), component);
}
// Example from http://engineering.blogfoster.com/higher-order-components-theory-and-practice/
const CheckedWelcome = composeComponents(
Welcome,
[
(c) => checkBoarded(c, { withValue: true, redirectTo: '/app/dashboard' }),
(c) => requiresAuth(c)
]
);
const CheckApp = composeComponents(
App,
[
(c) => checkBoarded(c, { withValue: false, redirectTo: '/welcome-on-board'}),
(c) => requiresAuth(c),
(c) => requiresSmth(c, { options: {} })
]
);
const routes = (
<Route path="">
// much more clean route definition
<Route path="/welcome-on-board" component={CheckedWelcome} />
<Route path="/app" component={CheckedApp}>
// ...
</Route>
</Route>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment