chore: migrate to reducer pattern instead of useState
This commit is contained in:
parent
18dd8b83f0
commit
7150ca87c3
7 changed files with 85 additions and 29 deletions
28
client/src/reducers/chatReducers.ts
Normal file
28
client/src/reducers/chatReducers.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { ChatAction, ChatState } from "../contexts/ChatContext";
|
||||
|
||||
export function chatReducer(state: ChatState, action: ChatAction): ChatState {
|
||||
switch (action.type) {
|
||||
case "ADD_MESSAGE":
|
||||
return {
|
||||
...state,
|
||||
messages: [...state.messages, action.payload],
|
||||
};
|
||||
case "SET_USER":
|
||||
return {
|
||||
...state,
|
||||
currentUser: action.payload,
|
||||
};
|
||||
case "SET_CONNECTED":
|
||||
return {
|
||||
...state,
|
||||
isConnected: action.payload,
|
||||
};
|
||||
case "CLEAR_MESSAGES":
|
||||
return {
|
||||
...state,
|
||||
messages: [],
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue