Skip to content

createGenericThunk

Generic thunk for async function

Import

import { createGenericThunk } from '@empeek-rnd-ui/redux/toolkit';

Usage

thunks.ts

export const fetchUsers = createGenericThunk(
  'users/fetchUsers',
  async () => {
      // async function axios/fetch to get your collection
  },
);

//in your component
dispatch(fetchUsers());

This thunk handle statuses of async function in the state. Also, it save error from response in the slice field errors.

thunkName - should be ${sliceName}/${thunkName}

Statuses

Slice saves thunk state in field statuses. Statuses can be:

enum ThunkStatusesTypes {
  loading = 'loading',
  finished = 'finished',
  error = 'error',
}