Last active
May 18, 2016 08:40
-
-
Save bultas/54a5d58dc7df31a6465df1f4e10bf50e to your computer and use it in GitHub Desktop.
Composable High-order components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function composeComponents(component, wrappers = []) { | |
return wrappers.reduce((c, wrapper) => wrapper(c), component); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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