Last active
February 21, 2017 12:38
-
-
Save danharper/fa9d0e71da2ea4672d17 to your computer and use it in GitHub Desktop.
three ways to "waitFor" a prop to become non-null before rendering
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
// 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