-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux.ts
49 lines (44 loc) · 1.08 KB
/
redux.ts
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
42
43
44
45
46
47
48
49
import {
combineReducers,
compose,
legacy_createStore as createStore,
} from "redux";
const initialState = {
user: {
isLoggingIn: false,
data: null,
},
posts: [],
};
const reducer = combineReducers({
// user: (state: any, action: any) => {},
// posts: (state: any, action: any) => {},
user: (state: any, action: any) => {
switch (action.type) {
case "LOGIN":
return {
isLoggingIn: true,
data: {
nickname: "baobabo",
password: "1234",
},
};
// or
// return [...state, action.data]
}
return state;
},
posts: (state: any, action: any) => {
return state;
},
});
// reducer는 상태를 바꾸는 규칙
// 액션이 디스패치되면 redcuer에 정의된 룰에 따라 state를 변화
const store = createStore(reducer, initialState);
store.dispatch({
type: "LOGIN",
data: { nickname: "BAOJYJ", password: "1234" },
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
store.getState(); // next state value