Skip to content

Instantly share code, notes, and snippets.

@danharper
Last active February 21, 2017 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharper/fa9d0e71da2ea4672d17 to your computer and use it in GitHub Desktop.
Save danharper/fa9d0e71da2ea4672d17 to your computer and use it in GitHub Desktop.
three ways to "waitFor" a prop to become non-null before rendering
// decorate the class, composition
function waitFor(prop) {
return ChildComponent => class extends Component {
render() {
return this.props[prop] ? <ChildComponent {...this.props} /> : null
}
}
}
// decorate the class, inheritance
function waitFor(prop) {
return ChildComponent => class extends ChildComponent {
render(...args) {
return this.props[prop] ? super.render(...args) : null
}
}
}
// decorate the render function directly
function waitFor(prop) {
return (target, key, descriptor) => {
return {
...descriptor,
value(...args) {
return this.props[prop] ? descriptor.value.apply(this, args) : null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment