-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredux-basics.js
45 lines (36 loc) · 918 Bytes
/
redux-basics.js
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
const redux = require('redux');
const createStore = redux.createStore;
const initialState = {
counter: 0,
name: "Kapil",
}
//Reducer
const rootReducer = (state = initialState, action) => {
if(action.type === 'INC_COUNTER'){
console.log({...state});
return {
...state,
counter: state.counter + 1,
}
}
if(action.type === 'ADD_COUNTER'){
console.log({...state});
return {
...state,
counter: state.counter + action.value,
}
}
return state;
}
//store
const store = createStore(rootReducer);
// console.log(store.getState());
//Subcription
store.subscribe(()=>{
console.log('[Subcription]',store.getState());
})
//Dispatching Action
store.dispatch({type: 'INC_COUNTER'});
console.log(store.getState());
store.dispatch({type: 'ADD_COUNTER', value: 10});
console.log(store.getState());