-
Notifications
You must be signed in to change notification settings - Fork 1
/
reducer.js
46 lines (37 loc) · 1.21 KB
/
reducer.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
45
46
import { html, render } from "https://unpkg.com/lit-html";
import { useReducer, withHooks } from "../wchooks.js";
function ExampleReducer() {
// advanced state initializer for a counter
function createCounter(dispatch, getState) {
return {
total: 0,
add(value) {
const state = getState();
const _ = state.total + value; // this is just to show that you can also access the current state if neededd
dispatch({ add: value });
},
sub(value) {
dispatch({ sub: value });
},
};
}
const counterReducer = (state, action) => {
if ("add" in action) {
return { ...state, total: state.total + action.add };
}
if ("sub" in action) {
return { ...state, total: state.total - action.sub };
}
return state;
};
const [counter] = useReducer(createCounter, counterReducer);
return html`
<fieldset>
<legend><b>useReducer</b></legend>
<button id="add" @click=${() => counter.add(2)}>+2</button>
<button id="sub" @click=${() => counter.sub(2)}>-2</button>
<span>= <b id="counter">${counter.total}</b></span>
</fieldset>
`;
}
customElements.define("example-reducer", withHooks(ExampleReducer, render));