Skip to content

Commit f3f5aa1

Browse files
committed
resolves #9
1 parent 5d357db commit f3f5aa1

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/.git
2+
/.vscode
13
/node_modules
24
/use-force-update.d.ts
35
/use-force-update.js

.npmignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
/.git
2+
/.vscode
13
/node_modules
24
/src
35
/tests
46
/.gitignore
57
/.npmignore
68
/.travis.yml
9+
/package-lock.json
710
/tsconfig.json
8-
/use-force-update.js.map
911
/yarn.lock

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "use-force-update",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"author": "Charles Stover <use-force-update@charlesstover.com>",
55
"bugs": {
66
"email" : "use-force-update@charlesstover.com",
77
"url": "https://github.com/CharlesStover/use-force-update/issues"
88
},
9-
"description": "React Hook to force your functional component to update.",
9+
"description": "React Hook to force your function component to update.",
1010
"directories": {
1111
"lib": "src",
1212
"test": "tests"

src/use-force-update.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { useCallback, useReducer } from 'react';
22

3+
type EmptyObject = {};
4+
35
type VoidFunction = () => void;
46

5-
const reducer = (state: boolean, _action: null): boolean => !state;
7+
// Returning a new object reference guarantees that a before-and-after
8+
// equivalence check will always be false, resulting in a re-render, even
9+
// when multiple calls to forceUpdate are batched.
10+
const reducer = (): EmptyObject => Object.create(null);
611

712
const useForceUpdate = (): VoidFunction => {
8-
const [ , dispatch] = useReducer<boolean, null>(reducer, true);
13+
const [ , dispatch] =
14+
useReducer<EmptyObject, null>(reducer, Object.create(null));
915

1016
// Turn dispatch(required_parameter) into dispatch().
1117
const memoizedDispatch = useCallback(

0 commit comments

Comments
 (0)