Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Created August 15, 2018 12:10
Show Gist options
  • Select an option

  • Save alexesDev/dcf2d82a615ae6dc8a565d1e19f3b7a5 to your computer and use it in GitHub Desktop.

Select an option

Save alexesDev/dcf2d82a615ae6dc8a565d1e19f3b7a5 to your computer and use it in GitHub Desktop.
antd + recompact: If the function passed in via the onClick prop return a Promise or if a promise is passed as an argument of the loading method, the component will automatically transition to its success or error states based on the outcome of the Promise without the need for external manipulation of state using a ref.
import { Button } from 'antd';
import { compose, omitProps, withState, withHandlers } from 'recompact';
const enhance = compose(
withState('loading', 'setLoading', false),
withHandlers({
onClick: props => e => {
if (!props.onClick) {
return;
}
const result = props.onClick(e);
if (result.then) {
const timeout = setTimeout(() => {
props.setLoading(true);
}, 200);
const finish = () => {
props.setLoading(false);
clearInterval(timeout);
};
result.then(finish).catch(finish);
}
}
}),
omitProps('setLoading'),
);
export default enhance(Button);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment