Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for custom equality function with multiple arguments to README #29

Merged
merged 2 commits into from
Aug 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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