import { createAsyncThunk } from "@reduxjs/toolkit"; import { APIname } from "APIname-file-path"; export const actionName = createAsyncThunk( "sliceName/actionName", async (params, thunkAPI) => { const { dispatch, getState, rejectWithValue} = thunkAPI // more information on redux document const { data } = params // action parameters when it is called let response; try { response = await APIname(); return { response }; } catch (err) { switch (err?.response?.status) { case 400: return Promise.reject(err?.response?.data); case 500: return Promise.reject(new Error("سرور با مشکل مواجه شده است")); default: return Promise.reject(new Error("اینترنت خود را چک کنید")); } } return rejectWithValue(err.response.data); } );
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter