Created
August 5, 2016 06:29
-
-
Save markshust/c5d8d3b35d3ffd7f056cd723dfed346c to your computer and use it in GitHub Desktop.
WIP: custom automagic composer for mobx + meteor
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, { createElement, Component, PropTypes } from 'react'; | |
| import { observer } from 'mobx-react'; | |
| import { _ } from 'meteor/underscore'; | |
| class Container extends Component { | |
| componentWillMount() { | |
| const { onMount } = this.props; | |
| if (typeof onMount === 'function') onMount(); | |
| } | |
| componentWillUnmount() { | |
| const { onUnmount } = this.props; | |
| if (typeof onUnmount === 'function') onUnmount(); | |
| } | |
| render() { | |
| const { renderChild, renderProps } = this.props; | |
| let { loadingComponent: result } = this.props; | |
| if (renderProps.isReady) { | |
| const cleanedProps = {}; | |
| Object.keys(renderProps).forEach(renderProp => { | |
| if (renderProps[renderProp] | |
| && renderProps[renderProp].$mobx | |
| && renderProps[renderProp].$mobx.array | |
| ) { | |
| cleanedProps[renderProp] = renderProps[renderProp] ? renderProps[renderProp].peek() : []; | |
| } else { | |
| cleanedProps[renderProp] = renderProps[renderProp]; | |
| } | |
| }); | |
| const props = Object.assign( | |
| {}, | |
| _.omit(this.props, 'renderChild', 'renderProps'), | |
| cleanedProps, | |
| ); | |
| result = createElement(renderChild, props); | |
| } | |
| return result; | |
| } | |
| } | |
| Container.propTypes = { | |
| loadingComponent: PropTypes.any, | |
| onMount: PropTypes.func, | |
| onUnmount: PropTypes.func, | |
| renderChild: PropTypes.any.isRequired, | |
| renderProps: PropTypes.object.isRequired, | |
| }; | |
| Container.defaultProps = { | |
| loadingComponent: <div>Loading...</div>, | |
| }; | |
| const mobxComposer = observer(Container); | |
| export { mobxComposer }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment