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 {useCallback, useState} from 'react'; | |
| import {Action, ActionCreator} from '@reduxjs/toolkit'; | |
| import {ThunkResult, useThunkDispatch} from '../redux/reducers'; | |
| export function useThunkAction<AC extends ActionCreator<ThunkResult<unknown>>, RT extends Awaited<ReturnType<ReturnType<AC>>>>( | |
| actionCreator: AC, | |
| ): [(...args: Parameters<AC>) => Promise<RT>, boolean] { | |
| const [isActionRunning, setRunning] = useState(false); | |
| const dispatch = useThunkDispatch(); | |
| const action = useCallback( |
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
| enum Demo { | |
| Eka = 100, | |
| Toka, | |
| } | |
| type DemoKey = keyof typeof Demo; | |
| // for validations | |
| const demoKeys = Object.values(Demo).filter((v) => isNaN(Number(v))) as [DemoKey]; | |
| const demoValues = Object.values(Demo).filter((v) => !isNaN(Number(v))) as [Demo]; |
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
| // export const useThunkDispatch = () => useDispatch<RootThunkDispatch>(); | |
| export function isPromise(action: unknown): action is Promise<unknown> { | |
| return typeof action === 'object' && 'catch' in (action as Promise<unknown>) && typeof (action as Promise<unknown>).catch === 'function'; | |
| } | |
| // hook action errors hook to component level and cause hook to throw component level error (=> to Error boundary) | |
| export function useThunkDispatch(): RootThunkDispatch { | |
| const dispatch = useDispatch<RootThunkDispatch>(); | |
| const [error, setError] = useState<unknown | undefined>(undefined); |