Last active
February 16, 2021 23:00
-
-
Save simonsmith/446955c7c9b2f7ce3599993b141d79e5 to your computer and use it in GitHub Desktop.
This file contains 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 {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, | |
}; | |
}; |
This file contains 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 {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