Skip to content

Instantly share code, notes, and snippets.

@selbekk
Created July 24, 2018 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selbekk/643842a11ac252445bade0835687b761 to your computer and use it in GitHub Desktop.
Save selbekk/643842a11ac252445bade0835687b761 to your computer and use it in GitHub Desktop.
A simple api state machine implementation
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