Created
July 24, 2018 13:19
-
-
Save selbekk/643842a11ac252445bade0835687b761 to your computer and use it in GitHub Desktop.
A simple api state machine implementation
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
const withApiState = TargetComponent => | |
class extends React.Component { | |
state = { | |
current: "idle" | |
}; | |
apiState = { | |
pending: () => this.setState({ current: "pending" }), | |
success: () => this.setState({ current: "success" }), | |
error: () => this.setState({ current: "error" }), | |
idle: () => this.setState({ current: "idle" }), | |
isPending: () => this.state.current === "pending", | |
isSuccess: () => this.state.current === "success", | |
isError: () => this.state.current === "error", | |
isIdle: () => this.state.current === "idle" | |
}; | |
render() { | |
return <TargetComponent {...this.props} apiState={this.apiState} />; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment