Skip to content

Commit

Permalink
Merge pull request #31 from streamich/createMemo
Browse files Browse the repository at this point in the history
Create memo
  • Loading branch information
streamich authored Oct 29, 2018
2 parents 25f6ea9 + 10c6906 commit 2abf53a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<br/>
<br/>
- [**State**](./docs/State.md)
- [`createMemo`](./docs/createMemo.md) &mdash; factory of memoized hooks.
- [`useGetSet`](./docs/useGetSet.md) &mdash; returns state getter `get()` instead of raw state.
- [`useGetSetState`](./docs/useGetSetState.md) &mdash; as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby.
- [`useObservable`](./docs/useObservable.md) &mdash; tracks latest value of an `Observable`.
Expand Down
36 changes: 36 additions & 0 deletions docs/createMemo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# `createMemo`

Hook factory, receives a function to be memoized, returns a memoized React hook,
which receives the same arguments and returns the same result as the original function.


## Usage

```jsx
import {createMemo} from 'react-use';

const fibonacci = n => {
if (n === 0) return 1;
if (n === 1) return 2;
return fibonacci(n - 1) + fibonacci(n - 2);
};

const useMemoFibonacci = createMemo(fibonacci);

const Demo = () => {
const result = useMemoFibonacci(10);

return (
<div>
fib(10) = {result}
</div>
);
};
```


## Reference

```js
const useMemoFn = createMemo(fn);
```
28 changes: 28 additions & 0 deletions src/__stories__/createMemo.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import {createMemo} from '..';
import ShowDocs from '../util/ShowDocs';

const fibonacci = n => {
if (n === 0) return 1;
if (n === 1) return 2;
return fibonacci(n - 1) + fibonacci(n - 2);
};

const useMemoFibonacci = createMemo(fibonacci);

const Demo = () => {
const result = useMemoFibonacci(10);

return (
<div>
fib(10) = {result}
</div>
);
};

storiesOf('createMemo', module)
.add('Docs', () => <ShowDocs md={require('../../docs/createMemo.md')} />)
.add('Demo', () =>
<Demo/>
)
5 changes: 5 additions & 0 deletions src/createMemo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {useMemo} from './react';

const createMemo = fn => (...args) => useMemo(() => fn(...args), args);

export default createMemo;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import createMemo from './createMemo';
import useAsync from './useAsync';
import useAudio from './useAudio';
import useBattery from './useBattery';
Expand Down Expand Up @@ -38,6 +39,7 @@ import useUpdate from './useUpdate';
import useWindowSize from './useWindowSize';

export {
createMemo,
useAsync,
useAudio,
useBattery,
Expand Down

0 comments on commit 2abf53a

Please sign in to comment.