-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathredux.ts
57 lines (49 loc) · 1.33 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
50
51
52
53
54
55
56
57
import {legacy_createStore as createStore, compose, applyMiddleware, Middleware} from 'redux';
import reducer from './reducers';
import {logIn, logOut} from './actions/user';
const initialState = {
user: {
isLoggingIn: true,
data: null,
},
posts: [] as string[],
};
const firstMiddleware: Middleware<{}, typeof initialState> = (store) => (next) => (action) => {
console.log('로깅', action);
next(action);
};
const thunkMiddleware: Middleware<{}, typeof initialState> = (store) => (next) => (action) => {
if (typeof action === 'function') { // 비동기
return action(store.dispatch, store.getState);
}
return next(action); // 동기
};
const enhancer = applyMiddleware(
firstMiddleware,
thunkMiddleware,
);
const store = createStore(reducer, initialState, enhancer);
console.log('1st', store.getState());
// --------------------------------------
store.dispatch(logIn({
id: 1,
name: 'zerocho',
admin: true,
}));
console.log('2nd', store.getState());
//
// store.dispatch(addPost({
// userId: 1,
// id: 1,
// content: '안녕하세요. 리덕스',
// }));
// console.log('3rd', store.getState());
// store.dispatch(addPost({
// userId: 1,
// id: 2,
// content: '두번째 리덕스',
// }));
// console.log('4th', store.getState());
//
// store.dispatch(logOut());
// console.log('5th', store.getState());