Skip to content

Commit

Permalink
feat: Avoid exhaustive dependencies in hooks
Browse files Browse the repository at this point in the history
Also:

- Update dependencies

Fixes #11, #12
  • Loading branch information
nathanbuchar committed Nov 7, 2019
1 parent f6bfa47 commit b10b0f9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
29 changes: 15 additions & 14 deletions package.json
Expand Up @@ -25,26 +25,27 @@
"scripts": {
"build": "rm -rf dist && webpack",
"prepare": "npm run build",
"pretest": "npm run build",
"release": "standard-version",
"release:beta": "standard-version --prerelease beta --skip.changelog",
"test": "jest test"
},
"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@testing-library/react": "^8.0.1",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@testing-library/react": "^9.3.2",
"@testing-library/react-hooks": "^3.2.1",
"babel-loader": "^8.0.5",
"eslint": "^5.3.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"jest": "^24.3.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-hooks-testing-library": "^0.5.1",
"react-test-renderer": "^16.8.6",
"standard-version": "^5.0.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3"
"eslint": "^6.6.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
"jest": "^24.9.0",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-test-renderer": "^16.11.0",
"standard-version": "^7.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"peerDependencies": {
"react": "^16.8.0",
Expand Down
28 changes: 16 additions & 12 deletions src/thunk-reducer.js
@@ -1,14 +1,14 @@
import { useRef, useState } from 'react';
import { useCallback, useRef, useState } from 'react';

/**
* @callback Thunk
* @function Thunk
* @param {Dispatch} dispatch
* @param {Function} getState
* @returns {void|*}
*/

/**
* @callback Dispatch
* @function Dispatch
* @param {Object|Thunk} action
* @returns {void|*}
*/
Expand All @@ -27,19 +27,23 @@ function useThunkReducer(reducer, initialArg, init = (a) => a) {

// State management.
const state = useRef(hookState);
const getState = () => state.current;
const setState = (newState) => {
const getState = useCallback(() => state.current, []);
const setState = useCallback((newState) => {
state.current = newState;
setHookState(newState);
};
}, []);

// Reducer and augmented dispatcher.
const reduce = (action) => reducer(getState(), action);
const dispatch = (action) => (
typeof action === 'function'
// Reducer.
const reduce = useCallback((action) => {
return reducer(getState(), action);
}, []);

// Augmented dispatcher.
const dispatch = useCallback((action) => {
return typeof action === 'function'
? action(dispatch, getState)
: setState(reduce(action))
);
: setState(reduce(action));
}, []);

return [hookState, dispatch];
}
Expand Down
2 changes: 1 addition & 1 deletion test/thunk-reducer.spec.js
@@ -1,6 +1,6 @@
/* eslint-env jest */

import { act, renderHook } from 'react-hooks-testing-library';
import { act, renderHook } from '@testing-library/react-hooks';
import { cleanup } from '@testing-library/react';

import useThunkReducer from '..';
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
libraryTarget: 'umd',
publicPath: '/dist/',
umdNamedDefine: true,
globalObject: 'typeof self !== \'undefined\' ? self : this', // Fixes #12
},
module: {
rules: [
Expand Down

0 comments on commit b10b0f9

Please sign in to comment.