Skip to content

Instantly share code, notes, and snippets.

@simonsmith
Last active February 16, 2021 23:00
Show Gist options
  • Save simonsmith/446955c7c9b2f7ce3599993b141d79e5 to your computer and use it in GitHub Desktop.
Save simonsmith/446955c7c9b2f7ce3599993b141d79e5 to your computer and use it in GitHub Desktop.
import {useState} from 'react';
export const requestStatus = {
IDLE: 'IDLE',
PENDING: 'PENDING',
REJECTED: 'REJECTED',
FULFILLED: 'FULFILLED',
};
export const useRequestStatus = () => {
const [status, setRequestStatus] = useState(requestStatus.IDLE);
return {
setStatusIdle: () => setRequestStatus(requestStatus.IDLE),
setStatusPending: () => setRequestStatus(requestStatus.PENDING),
setStatusFulfilled: () => setRequestStatus(requestStatus.FULFILLED),
setStatusRejected: () => setRequestStatus(requestStatus.REJECTED),
isStatusIdle: status === requestStatus.IDLE,
isStatusPending: status === requestStatus.PENDING,
isStatusFulfilled: status === requestStatus.FULFILLED,
isStatusRejected: status === requestStatus.REJECTED,
};
};
import {useState} from 'react';
export const requestStatus = {
IDLE: 'IDLE',
PENDING: 'PENDING',
REJECTED: 'REJECTED',
FULFILLED: 'FULFILLED',
} as const;
export type RequestStatus = typeof requestStatus[keyof typeof requestStatus];
export const useRequestStatus = () => {
const [status, setRequestStatus] = useState<RequestStatus>(
requestStatus.IDLE
);
return {
setStatusIdle: () => setRequestStatus(requestStatus.IDLE),
setStatusPending: () => setRequestStatus(requestStatus.PENDING),
setStatusFulfilled: () => setRequestStatus(requestStatus.FULFILLED),
setStatusRejected: () => setRequestStatus(requestStatus.REJECTED),
isStatusIdle: status === requestStatus.IDLE,
isStatusPending: status === requestStatus.PENDING,
isStatusFulfilled: status === requestStatus.FULFILLED,
isStatusRejected: status === requestStatus.REJECTED,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment