Last active
September 19, 2017 01:54
-
-
Save joedski/993a2085ec15cf385c2c1e4e25770f20 to your computer and use it in GitHub Desktop.
hof thunks: A connected component
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
import { myEndpoint } from 'my-app/api/thunks'; | |
// ... other sundry imports. | |
export default compose( | |
connect( | |
null, | |
(dispatch, ownProps) => ({ | |
getMyResource: dispatch(myEndpoint({ | |
name: ownProps.name, | |
order: 'asc', | |
})), | |
}) | |
) | |
)(class MyResourceDisplay extends PureComponent { | |
handleClick(event) { | |
event.preventDefault(); | |
const { result, error } = | |
await this.props.getMyResource({ | |
name: 'foo', | |
order: 'asc', | |
}); | |
if (error) { | |
this.setState({ | |
status: 'error', | |
lastError: error, | |
// not clearing result. | |
}); | |
} | |
else { | |
this.setState({ | |
status: 'success', | |
hasReceivedBefore: true, | |
lastError: null, | |
result, | |
}); | |
} | |
} | |
// ... other componenty stuff here. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment