-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested-state.js
More file actions
50 lines (42 loc) · 1.12 KB
/
nested-state.js
File metadata and controls
50 lines (42 loc) · 1.12 KB
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
const redux = require("redux");
const createStore = redux.createStore;
const produce = require('immer').produce;
const initialState = {
name: "Name1",
address: {
street: "123 Main Street",
city: "Boston",
state: "MA",
},
};
const UPDATE_STREET = "UPDATE_STREET";
function updateStreet (street) {
return {
type: UPDATE_STREET,
payload: street,
}
}
const reducer = (state = initialState, actions) => {
switch (actions.type) {
case UPDATE_STREET: {
// return {
// ...state,
// address: {
// ...state.address,
// street: actions.payload
// }
// }
return produce(state, (draft) => {
draft.address.street = actions.payload;
})
}
default: return state;
}
}
const store = createStore(reducer);
console.log("Initial state", store.getState());
const unsubscribe = store.subscribe(() => {
console.log("Updated state", store.getState())
});
store.dispatch(updateStreet("456 Main Street"));
unsubscribe();