Last active
August 29, 2015 14:19
-
-
Save caasi/952cf9aa3a6547c1c1e6 to your computer and use it in GitHub Desktop.
another Container inspired by AltContainer
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
import React from 'react'; | |
import { PureRenderMixin } from 'react/addons'; | |
import ListenerMixin from 'alt/mixins/ListenerMixin'; | |
import { keys, isArray } from 'lodash-compat'; | |
var Container = React.createClass({ | |
mixins: [PureRenderMixin, ListenerMixin], | |
getDefaultProps() { | |
return { | |
stores: {}, | |
transform: stores => {} | |
}; | |
}, | |
getInitialState() { | |
var { stores } = this.props; | |
var state = {}; | |
for (let name in stores) { | |
let store = stores[name]; | |
state[name] = store.getState(); | |
} | |
return state; | |
}, | |
componentDidMount() { | |
var { stores } = this.props; | |
for (let name in stores) { | |
let store = stores[name]; | |
this.listenTo(store, () => this.setState({[name]: store.getState()})); | |
} | |
}, | |
render() { | |
var { children, transform } = this.props; | |
return React.addons.cloneWithProps( | |
React.Children.only(children), | |
transform(this.state) | |
); | |
} | |
}); | |
export default Container; |
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
{ | |
render() { | |
return ( | |
<Container | |
stores={{ FooStore, BarStore }} | |
transform={({ FooStore, BarStore }) => { | |
var { foo = [] } = FooStore; | |
var { bar = [] } = BarStore; | |
return { foo, bar }; | |
}}> | |
<Foobar /> | |
</Container> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment