Last active
January 18, 2018 19:32
-
-
Save tortillaj/8b24159547cea434c398ddcd05e46ad0 to your computer and use it in GitHub Desktop.
React Recompose blog post
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
export default class Button extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
isLoading: false, | |
} | |
} | |
static propTypes = { | |
onClick: PropTypes.func, | |
className: PropTypes.string, | |
type: PropTypes.string, | |
children: PropTypes.any.isRequired, | |
} | |
static defaultProps = { | |
type: 'button', | |
} | |
doSomething = (e) => { | |
if (!this.props.onClick) return | |
this.setState({ isLoading: true }) | |
this.props.onClick(e).then(() => this.setState({ isLoading: false ])) | |
} | |
render() { | |
const { type, className, children ) = this.props | |
return ( | |
<button type={type} className={className} onClick={this.doSomething}> | |
{this.state.isLoading && <Loading />} | |
{!this.state.isLoading && children} | |
</button> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment