Skip to content

Commit

Permalink
adding distinct parameter to the useValueHistory hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Russo committed Aug 15, 2020
1 parent 25722f2 commit 3a293bc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,13 @@ online
- useTimeout clear function is now correctly used as useEffect cleanup
- CI minor issues

## [0.27.4] - 2020-08-15

### Added
- `useValueHistory` can now be used with distinct history

### Fixed

- dependencies update
- CI minor issues

2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export declare const useConditionalTimeout: (fn: Function, milliseconds: number,
/**
* useValueHistory
*/
export declare const useValueHistory: (value: any) => Array<any>;
export declare const useValueHistory: (value: any, distinct?: boolean) => Array<any>;


/**
Expand Down
33 changes: 13 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beautiful-react-hooks",
"version": "0.27.3",
"version": "0.27.4",
"description": "A collection of beautiful (and hopefully useful) React hooks to speed-up your components and hooks development",
"main": "dist/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -48,7 +48,7 @@
"@babel/preset-env": "^7.11.0",
"@babel/preset-react": "^7.10.4",
"@babel/register": "^7.10.5",
"@rollup/plugin-node-resolve": "8.4.0",
"@rollup/plugin-node-resolve": "9.0.0",
"@testing-library/react": "10.4.8",
"@testing-library/react-hooks": "^3.4.1",
"babel-eslint": "^10.1.0",
Expand All @@ -59,12 +59,12 @@
"chai": "^4.2.0",
"coveralls": "3.1.0",
"css-loader": "^4.2.1",
"eslint": "^7.6.0",
"eslint": "^7.7.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-plugin-chai-expect": "^2.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.5",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.0.8",
"glob": "^7.1.6",
"husky": "^4.2.5",
Expand All @@ -78,7 +78,7 @@
"react-dom": "^16.12.0",
"react-styleguidist": "^11.0.8",
"react-test-renderer": "^16.13.1",
"rollup": "2.23.1",
"rollup": "2.26.0",
"rollup-plugin-babel": "4.4.0",
"sinon": "^9.0.3",
"style-loader": "1.2.1",
Expand Down
8 changes: 7 additions & 1 deletion src/useValueHistory.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { useRef, useEffect } from 'react';

const distinctValues = (value, current, array) => array.indexOf(value) === current;

/**
* Accepts a variable (possibly a prop or a state) and returns its history (changes through updates).
*/
const useValueHistory = (value) => {
const useValueHistory = (value, distinct = false) => {
const history = useRef([]);

// quite simple
useEffect(() => {
history.current.push(value);

if (distinct) {
history.current.filter(distinctValues);
}
}, [value]);

return history.current;
Expand Down

0 comments on commit 3a293bc

Please sign in to comment.