Created
July 30, 2015 14:03
-
-
Save roman01la/b44f32460783521fa4e8 to your computer and use it in GitHub Desktop.
Maybe monad in React
This file contains hidden or 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
// 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