Skip to content

Commit

Permalink
Add example for custom equality function with multiple arguments to R…
Browse files Browse the repository at this point in the history
…EADME (#29)

* Update README.md

* Update README.md
  • Loading branch information
andwilley authored and alexreardon committed Aug 7, 2018
1 parent a18436e commit 58fb93b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ Here is the expected [flow](http://flowtype.org) type signature for a custom equ
type EqualityFn = (a: mixed, b: mixed) => boolean;
```

#### Custom equality function with multiple arguments

If the function you want to memoize takes multiple arguments, your custom equality function will be called once for each argument and will be passed each argument's new value and last value.

```js
import memoizeOne from 'memoize-one';

const makeCountObj = (first, second, third) => ({
first: first.count,
second: second.count,
third: third.count,
});

const areCountPropertiesEqual = (newArg, lastArg) => newArg.count === lastArg.count;
// runs once for first's new and last values, once for second's, etc.

const memoizedMakeCountObj = memoizeOne(makeCountObj, areCountPropertiesEqual);

const result1 = memoizedMakeCountObj(
{a: '?', count: 1},
{a: '$', count: 2},
{a: '#', count: 3}
);
const result2 = memoizedMakeCountObj(
{b: null, count: 1},
{b: null, count: 2},
{b: null, count: 3}
);

result1 === result2; // true - same reference
```

## Installation

```bash
Expand Down

0 comments on commit 58fb93b

Please sign in to comment.