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

Create memo #31

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
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
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