Last active
April 8, 2016 13:49
-
-
Save fab1an/a745e2b0767917b60e8b9a0848403ac8 to your computer and use it in GitHub Desktop.
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
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