Skip to content

Instantly share code, notes, and snippets.

@Rishit30G
Created August 3, 2023 12:08
Show Gist options
  • Select an option

  • Save Rishit30G/f7a864ddca7e111f1a63fc56eb91f932 to your computer and use it in GitHub Desktop.

Select an option

Save Rishit30G/f7a864ddca7e111f1a63fc56eb91f932 to your computer and use it in GitHub Desktop.
MessageSlice.tsx
import {createSlice} from '@reduxjs/toolkit';
//! USER LIST->All,active/current ,loading,error ,setUser,setUsers
//! USER MESSAGES (MESSAGE SLICE)
const userlistSlice = createSlice({
name: 'userlist',
initialState: {
users: [],
user: {},
currentUser: {},
active: false,
loading: false,
error: null,
},
reducers: {
setUsers: (state, action) => {
state.users = action.payload;
},
setUser: (state, action) => {
state.user = action.payload;
},
setCurrentUser: (state, action) => {
state.currentUser = action.payload;
},
setActive: (state, action) => {
state.active = action.payload;
},
setLoading: (state, action) => {
state.loading = action.payload;
},
setError: (state, action) => {
state.error = action.payload;
},
}
});
const userMessagesSlice = createSlice({
name: 'userMessages',
initialState: {
messages: [],
message: {},
loading: false,
error: null,
},
reducers: {
setMessages: (state, action) => {
state.messages = action.payload;
},
setMessage: (state, action) => {
state.message = action.payload;
},
setLoading: (state, action) => {
state.loading = action.payload;
},
setError: (state, action) => {
state.error = action.payload;
},
},
});
export {userlistSlice, userMessagesSlice};
export const {setUsers, setUser, setCurrentUser, setActive, setLoading, setError} = userlistSlice.actions;
export const {setMessages, setMessage} = userMessagesSlice.actions;
@geeky-abhishek
Copy link

LGTM ,the only thing want to add is to create separate files for each slice

@Rishit30G
Copy link
Author

Sure, done that ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment