Utility belt for react redux app
npm install @breadhead/thunk-utilsUtility belt for react redux app
#### Usage
``
const article = useMemoState(
() => selectArticleById(id),
() => fetchArticlePage(id),
[id],
)
`
#### Type
``
useMemoState:
createSelector: () => (state: State) => T,
refetchAction: () => ThunkAction
deps: any[],
) => T
#### Usage
`
import { createFetchOrFail } from '@breadhead/thunk-utils'
import { State } from './State'
import { Api } from '../api'
export const fetchOrFail = createFetchOrFail
-----------------------
import { fetchLecturesRequest } from '../api/fetchLecturesRequest'
import { actions } from '../reducer/list'
import { fetchOrFail } from '@app/domain/store/fetchOrFail'
export const fetchLecturesList = () =>
fetchOrFail(actions.fetching, async (dispatch, getApi) => {
const lectures = await fetchLecturesRequest(getApi())()
await dispatch(actions.data.addLectures(lectures))
})
`
#### Type
``
createFetchOrFail:
fetchActions: FetchActions,
execute: Execute
) => (
dispatch: ThunkDispatch
getState: () => State,
createApi: (token: Option
) => Promise
#### Usage
`
const dispatch = useThunk()
const addToFavs = useCallback(async () => {
await dispatch(addToFavorites(id, type))
}, [id, type, dispatch])
`
#### Type
``
useThunk: () =>
action: ThunkAction
) => Promise
#### Usage
`
import {
ClearAction,
createClearReduxWithFetching,
WithFetchingState,
} from 'redux-clear'
import { uniqBy } from 'lodash'
import { ArticleModel } from '@app/models/article/ArticleModel'
type InternalState = ArticleModel[]
export type State = WithFetchingState
export interface Actions {
addArticle: ClearAction<[ArticleModel]>
}
const { reducer, actions } = createClearReduxWithFetching<
InternalState,
Actions
>(
{
addArticle: state => article => uniqBy([...state, article], 'id'),
},
[],
'article',
)
export { reducer, actions }
`
#### Type
``
createClearReduxWithFetching:
actionsConfig: ActionsConfig
initialState: State,
tag?: string,
) => {
reducer: Reducer<
{
fetching: import('.').FetchingState
data: State
},
AnyAction
>
actions: {
fetching: {
request: () => AnyAction
failure: (args_0: string) => AnyAction
success: () => AnyAction
}
data: {
[key in keyof Actions]: (
...args: import('./utils/ArrayOrUnknown').ArrayOrUnknown
) => AnyAction
}
}
}
#### Usage
`
import { createFetchingRedux, FetchingState } from 'redux-clear'
export type State = FetchingState
const { reducer, actions } = createFetchingRedux('login/')
export { reducer, actions }
`
#### Type
```
createFetchingRedux: (
key: string,
) => ClearRedux