forked from SNU-SWPP22-TA/swpp-p4-redux-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredux-basics.js
More file actions
21 lines (19 loc) · 739 Bytes
/
redux-basics.js
File metadata and controls
21 lines (19 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const { configureStore } = require('@reduxjs/toolkit'); // load module in Node.js
const initialState = { number: 0 }; // default state
// create identity reducer
const reducer = (state = initialState, action) => {
if (action.type === 'ADD') { return ({ ...state, number: state.number + 1}); }
else if (action.type === 'ADD_VALUE') {
return ({...state, number: state.number + action.value});
}
return state;
}
// create redux store
const store = configureStore({ reducer: reducer });
console.log(store.getState());
store.dispatch({ type: 'ADD' });
store.dispatch({ type: 'ADD_VALUE', value: 5 });
console.log(store.getState());
store.subscribe(() => {
console.log('[Subscription]', store.getState());
});