Skip to content

Commit 5a29877

Browse files
authored
Merge pull request #28 from nbrady-techempower/back-to-the-future
Never a stale moment
2 parents f014c98 + 1dfa64b commit 5a29877

File tree

8 files changed

+175
-156
lines changed

8 files changed

+175
-156
lines changed

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
![Open Issues](https://img.shields.io/github/issues-raw/techempower/react-governor.svg)
66
![Build Status](https://travis-ci.org/TechEmpower/react-governor.svg?branch=master)
77

8-
98
Use a governor hook to manage state with actions for, and created by, the people.
109

1110
Available as an [npm package](https://www.npmjs.com/package/@techempower/react-governor).
@@ -22,15 +21,15 @@ build the boilerplate of `actions`, `dispatch`, and `reducer`.
2221
const initialState = { count: 1 };
2322

2423
const contract = {
25-
increment(state) {
26-
return {
27-
count: state.count + 1
28-
};
24+
increment() {
25+
return () => ({
26+
count: this.state.count + 1
27+
});
2928
},
30-
add(val, state) {
31-
return {
32-
count: state.count + val
33-
}
29+
add(val) {
30+
return () => ({
31+
count: this.state.count + val
32+
});
3433
}
3534
}
3635

@@ -48,7 +47,7 @@ export default function Counter() {
4847
}
4948
```
5049

51-
[Test that this works](https://codesandbox.io/s/hopeful-shannon-lz433)
50+
[Test that this works](https://codesandbox.io/s/focused-borg-4rrsh)
5251

5352
This should feel very similar to how `useReducer` works with actions and
5453
reducers.
@@ -58,8 +57,8 @@ These actions are functions which take in any number of arguments and the
5857
current state. These actions are responsible for returning an object that
5958
describes what in the state should be mutated.
6059

61-
As from our example, the `increment` action returns an object describing that
62-
the state should be mutated such that `count` is `state.count + 1`. Similarly,
60+
As from our example, the `increment` action returns a reducer function describing that
61+
the state should be mutated such that `count` is `this.state.count + 1`. Similarly,
6362
the `add` action returns an object describing that the state should be mutated
64-
such that `count` is `state.count + val`, and notice that when we called `add`
63+
such that `count` is `this.state.count + val`, and notice that when we called `add`
6564
that we passed it a value to add.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@techempower/react-governor",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "The easiest way to govern over your application's state and actions.",
55
"license": "BSD-3",
66
"repository": {

src/examples/counter/Counter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function Counter() {
2020
onClick={() => actions.addNewState("Hello")}
2121
/>
2222
<button className="removeState" onClick={() => actions.removeState()} />
23-
<button className="asyncFunc" onClick={actions.asyncFunc} />
23+
<button className="asyncFunc" onClick={() => actions.asyncFunc()} />
2424
<button
2525
className="compoundAsyncFunc"
2626
onClick={() => {
@@ -31,7 +31,7 @@ export default function Counter() {
3131
}}
3232
/>
3333
<input className="googleStatus" value={state.status} />
34-
<button className="fetchGoogle" onClick={actions.fetchGoogle} />
34+
<button className="fetchGoogle" onClick={() => actions.fetchGoogle()} />
3535
</div>
3636
);
3737
}

src/examples/counter/CounterContract.js

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@ export const initialState = {
55
};
66

77
export const contract = {
8-
increment(state) {
9-
return {
10-
count: state.count + 1
11-
};
8+
increment() {
9+
return () => ({
10+
count: this.state.count + 1
11+
});
1212
},
13-
decrement(state) {
14-
return {
15-
count: state.count - 1
16-
};
13+
decrement() {
14+
return () => ({
15+
count: this.state.count - 1
16+
});
1717
},
18-
add(val, state) {
19-
return {
20-
count: state.count + val
21-
};
18+
add(val) {
19+
return () => ({
20+
count: this.state.count + val
21+
});
2222
},
2323
set(val) {
24-
return {
24+
return () => ({
2525
count: val
26-
};
26+
});
2727
},
28-
addSum(val, val2, state) {
29-
return {
30-
count: state.count + val + val2
31-
};
28+
addSum(val, val2) {
29+
return () => ({
30+
count: this.state.count + val + val2
31+
});
3232
},
33-
addNewState(val, state) {
34-
return {
35-
...state,
33+
addNewState(val) {
34+
return () => ({
35+
...this.state,
3636
newState: val
37-
};
37+
});
3838
},
3939
removeState() {
40-
return {};
40+
return () => ({});
4141
},
4242
async asyncFunc() {
4343
// Block with a promise that resolved a new count
@@ -48,8 +48,8 @@ export const contract = {
4848
);
4949

5050
// set the state count to our promised count
51-
return state => ({
52-
count: state.count + count
51+
return () => ({
52+
count: this.state.count + count
5353
});
5454
},
5555
async fetchGoogle() {
@@ -58,10 +58,5 @@ export const contract = {
5858
return () => ({
5959
status: google.status
6060
});
61-
},
62-
statedInc(state) {
63-
return {
64-
count: state.count + 1
65-
};
6661
}
6762
};

src/examples/render-counting/Counter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ export default function Counter() {
1313
actions.multiply(0);
1414
actions.add(2);
1515
actions.add(6);
16-
}, []);
16+
}, [actions]);
1717

1818
useEffect(() => {
1919
if (state.count === 2) {
2020
actions.set(-1);
2121
}
22-
}, [state.count]);
22+
}, [actions, state.count]);
2323

2424
useEffect(() => {
2525
if (state.count === 8) {
2626
actions.divide(2);
2727
}
28-
}, [state.count]);
28+
}, [actions, state.count]);
2929

3030
numRenders++;
3131

src/examples/render-counting/CounterContract.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ export const initialState = {
33
};
44

55
export const contract = {
6-
add(val, state) {
7-
return {
8-
count: state.count + val
9-
};
6+
add(val) {
7+
return () => ({
8+
count: this.state.count + val
9+
});
1010
},
1111
set(val) {
12-
return {
12+
return () => ({
1313
count: val
14-
};
14+
});
1515
},
16-
multiply(val, state) {
17-
return {
18-
count: state.count * val
19-
};
20-
},
21-
divide(val, state) {
22-
return {
23-
count: state.count / val
24-
};
16+
multiply(val) {
17+
return () => ({
18+
count: this.state.count * val
19+
});
2520
},
21+
divide(val) {
22+
return () => ({
23+
count: this.state.count / val
24+
});
25+
}
2626
};

src/examples/simple-counter/SimpleCounter.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import React from "react";
22
import { useGovernor } from "../..";
33

44
const contract = {
5-
increment(state) {
6-
return state + 1;
5+
increment() {
6+
return () => this.state + 1;
77
},
8-
decrement(state) {
9-
return state - 1;
8+
decrement() {
9+
return () => this.state - 1;
1010
},
11-
add(num, state) {
12-
return state + num;
11+
add(num) {
12+
return () => this.state + num;
1313
},
14-
subtract(num, state) {
15-
return state - num;
14+
subtract(num) {
15+
return () => this.state - num;
1616
}
1717
};
1818

0 commit comments

Comments
 (0)