-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredux-basics.js
More file actions
41 lines (32 loc) · 804 Bytes
/
redux-basics.js
File metadata and controls
41 lines (32 loc) · 804 Bytes
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
const redux = require('redux');
const createStore = redux.createStore;
const initialState = {
counter: 0
}
//Reducer
const rootReducer = (state = initialState , action) => {
if(action.type === 'INC_COUNTER'){
return {
...state,
counter: state.counter + 1
};
}
if(action.type === 'ADD_COUNTER'){
return {
...state,
counter: state.counter + action.value
};
}
return state;
};
//Store
const store = createStore(rootReducer);
console.log(store.getState());
// Subscription
store.subscribe(() => {
console.log('[Subscription]',store.getState());
});
// Dispatching Action
store.dispatch({type: 'INC_COUNTER'});
store.dispatch({type: 'ADD_COUNTER', value: 10});
console.log(store.getState());