Last active
January 26, 2024 03:44
-
-
Save eimg/ceac3b3161ed96d886f33b3769039f5a to your computer and use it in GitHub Desktop.
RTK Starter Template - Type 1
This file contains 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
/*** mySlice.js ***/ | |
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; | |
export const asyncMethod = createAsyncThunk("action/id", async () => { | |
// fetch and return data | |
// will become action payload | |
}); | |
export const mySlice = createSlice({ | |
name: "name", | |
initialState: { | |
status: "idle", | |
// other states | |
}, | |
reducers: { | |
method: (state, action) => { | |
// mutate state here | |
}, | |
// other action methods | |
}, | |
extraReducers: builder => { | |
builder | |
.addCase(asyncMethod.pending, state => { | |
state.status = "pending"; | |
}) | |
.addCase(asyncMethod.fulfilled, (state, action) => { | |
state.status = "idle"; | |
// mutate state here, use action payload | |
// which is return value of asyncMethod | |
}) | |
.addCase(asyncMethod.rejected, state => { | |
state.status = "rejected"; | |
}); | |
}, | |
}); | |
export const { method } = mySlice.actions; | |
export default mySlice.reducer; | |
/*** store.js ***/ | |
// import { configureStore } from "@reduxjs/toolkit"; | |
// import myReducer from "./mySlice"; | |
// | |
// export const store = configureStore({ | |
// reducer: { | |
// app: myReducer, | |
// }, | |
// }); | |
/*** main.jsx */ | |
// import { Provider } from "react-redux"; | |
// import { store } from "./store"; | |
// | |
// <Provider store={store}> | |
// <YourApp /> | |
// </Provider> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment