Created
August 6, 2015 10:26
-
-
Save danielmahal/22a3921eeec5378616e3 to your computer and use it in GitHub Desktop.
Composing components?
This file contains 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
import React from 'react'; | |
import reduce from 'lodash/collection/reduce'; | |
function compose(Component, containers) { | |
return reduce(containers, (ComposedComponent, containerCreator) => { | |
return containerCreator(ComposedComponent); | |
}, Component); | |
} | |
function createContainer(containerCreator) { | |
return function lazyContainerCreator(Component, ...args) { | |
return containerCreator(Component, ...args); | |
}; | |
} | |
const authenticationContainer = createContainer((Component) => { | |
return class extends React.Component { | |
// Check authentication | |
render() { | |
return React.createElement(Component, this.props); | |
} | |
}; | |
}); | |
const firebaseContainer = createContainer((Component, params) => { | |
// Get stuff from firebase | |
return class extends React.Component { | |
render() { | |
params; | |
return React.createElement(Component, this.props); | |
} | |
}; | |
}); | |
class Dashboard extends React.Component { | |
render() { | |
return null; | |
} | |
} | |
compose(Dashboard, [ | |
authenticationContainer(), | |
userContainer(), | |
firebaseContainer({ | |
subscribeTo: { | |
feed: () => `/feed`, | |
}, | |
}), | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment