Skip to content

Instantly share code, notes, and snippets.

@fab1an
Last active April 8, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fab1an/a745e2b0767917b60e8b9a0848403ac8 to your computer and use it in GitHub Desktop.
Save fab1an/a745e2b0767917b60e8b9a0848403ac8 to your computer and use it in GitHub Desktop.
class ReactBase extends React.Component {
/* event emitting function, for completeness' sake */
emit(eventName, eventData = {}) {
this.props.dispatch(eventName, eventData);
}
shouldComponentUpdate(nextProps) {
return !_.isEqual(this.props, nextProps);
}
static makePropTree(state, dispatch, id) {
/* call this component's static "makeProps" function, dispatch is a callback to handle events */
const props = this.makeProps(state, dispatch, id);
/* work on the returned object, this is where the magic happens */
return _.mapValues(props, value => {
/* if another component is returned, use that component's "makePropTree" to construct the subtree */
if (_.isObject(value) && _.isFunction(value.makePropTree))) {
return value.makePropTree(state, dispatch);
/* two dimensional array for type with id */
} else if (_.isArray(value) && value.length === 2) {
const [subComp, id] = value;
if (_.isObject(subComp) && _.isFunction(subComp.makePropTree))) {
return subComp.makePropTree(state, dispatch, id);
}
}
return value;
});
}
}
/* rendering */
const propTree = App.makePropTree(state, dispatchFn);
ReactDOM.render(<App {...propTree}/>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment