-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlice.jsx
41 lines (37 loc) · 1.08 KB
/
Slice.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// app/reducers-or-slices/speceficDataFetcher-slice.jsx
import { createSlice } from "@reduxjs/toolkit";
import { createAsyncThunk } from "@reduxjs/toolkit";
export const fetchData = createAsyncThunk(
"data-fetcher-thunk",
async (_, { rejectWithValue }) => {
try {
const response = await fetch(`https://your-api.com/content`);
const data = await response.json();
return data;
} catch (error) {
rejectWithValue(error);
console.error(error);
}
}
);
const myslice = createSlice({
name: "myslice-name",
initialState: {
data: null,
loading: false,
},
reducers: {},
extraReducers: (builder) => {
builder.addCase(BestSalesfetchAction.pending, (state) => {
state.loading = true;
});
builder.addCase(BestSalesfetchAction.fulfilled, (state, action) => {
state.loading = false;
state.data = action.payload;
});
builder.addCase(BestSalesfetchAction.rejected, (state, action) => {
state.loading = false;
});
},
});
export default BestSalesSlice.reducer;