Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
React Recompose blog post
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