Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created July 30, 2015 14:03
Show Gist options
  • Save roman01la/b44f32460783521fa4e8 to your computer and use it in GitHub Desktop.
Save roman01la/b44f32460783521fa4e8 to your computer and use it in GitHub Desktop.
Maybe monad in React
// this will fail on if there's no data
React.createClass({
render() {
return <div>renderComplexWidget(this.props.data)</div>;
}
});
// this will not; if no data — return null
React.createClass({
render() {
return <div>{Maybe(this.props.data).maybe(null, renderComplexWidget)}</div>;
}
});
// ugly
React.createClass({
render() {
return <div>{this.props.data ? renderComplexWidget(this.props.data) : null}</div>;
}
});
// even worse
React.createClass({
render() {
const data = this.props.data;
return <div>{typeof data === 'object' && data !== null ? renderComplexWidget(data) : null}</div>;
}
});
// above could be
React.createClass({
render() {
return <div>{MaybeObject(this.props.data).maybe(null, renderComplexWidget)}</div>;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment