-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
39 lines (31 loc) · 1.04 KB
/
main.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
// Actions
const CHANGE_BG_COLOR = "CHANGE_BG_COLOR";
// The store's initial state
const INITIAL_STATE = {backgroundColor: 'purple'};
// Reducer will be subscribed to listen for any actions on the store
// and should return a new state to be published to the state queue
function bgApp(state, action) {
switch (action.type){
case CHANGE_BG_COLOR:
return {
backgroundColor: action.backgroundColor
}
default:
return state;
}
}
// Subscribe reducers to store when the store is created
let store = Redux.createStore(bgApp, INITIAL_STATE);
// Subscribe to state changes
var manageBackground = function() {
console.log("changing background color");
console.log('get state', store.getState());
document.bgColor = store.getState().backgroundColor;
};
store.subscribe(manageBackground);
// Publish Event Message (action)
document.getElementById('change-color').addEventListener('click', function(){
store.dispatch({type: CHANGE_BG_COLOR, backgroundColor: "green"});
});
// Kickoff a background change
manageBackground();