From 8730eaa4413ed9655abe5acc678786fc1a1f1d33 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Oct 2018 20:50:08 +0100 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20createMemo=20h?= =?UTF-8?q?ook=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + docs/createMemo.md | 28 ++++++++++++++++++++++++++++ src/__stories__/createMemo.story.tsx | 28 ++++++++++++++++++++++++++++ src/createMemo.ts | 5 +++++ src/index.ts | 2 ++ 5 files changed, 64 insertions(+) create mode 100644 docs/createMemo.md create mode 100644 src/__stories__/createMemo.story.tsx create mode 100644 src/createMemo.ts diff --git a/README.md b/README.md index 0358f31630..5f7272f929 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@

- [**State**](./docs/State.md) + - [`createMemo`](./docs/createMemo.md) — creates a memoized hook. - [`useGetSet`](./docs/useGetSet.md) — returns state getter `get()` instead of raw state. - [`useGetSetState`](./docs/useGetSetState.md) — as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby. - [`useObservable`](./docs/useObservable.md) — tracks latest value of an `Observable`. diff --git a/docs/createMemo.md b/docs/createMemo.md new file mode 100644 index 0000000000..0fc5c1f0ab --- /dev/null +++ b/docs/createMemo.md @@ -0,0 +1,28 @@ +# `createMemo` + +A factory that returns a React hook, which is memoized for each set of arguments passed to it. + + +## 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 ( +
+ fib(10) = {result} +
+ ); +}; +``` diff --git a/src/__stories__/createMemo.story.tsx b/src/__stories__/createMemo.story.tsx new file mode 100644 index 0000000000..9a07ddb2f6 --- /dev/null +++ b/src/__stories__/createMemo.story.tsx @@ -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 ( +
+ fib(10) = {result} +
+ ); +}; + +storiesOf('createMemo', module) + .add('Docs', () => ) + .add('Demo', () => + + ) diff --git a/src/createMemo.ts b/src/createMemo.ts new file mode 100644 index 0000000000..59405a81a9 --- /dev/null +++ b/src/createMemo.ts @@ -0,0 +1,5 @@ +import {useMemo} from './react'; + +const createMemo = fn => (...args) => useMemo(() => fn(...args), args); + +export default createMemo; diff --git a/src/index.ts b/src/index.ts index b57e7a86f8..679c0d3477 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import createMemo from './createMemo'; import useAsync from './useAsync'; import useAudio from './useAudio'; import useBattery from './useBattery'; @@ -38,6 +39,7 @@ import useUpdate from './useUpdate'; import useWindowSize from './useWindowSize'; export { + createMemo, useAsync, useAudio, useBattery, From 10c6906e0b647952cb39866b5c47be1bc8f87eb8 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Oct 2018 20:54:07 +0100 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20=E2=9C=8F=EF=B8=8F=20improve=20crea?= =?UTF-8?q?teMemo=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/createMemo.md | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5f7272f929..2270830ff9 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@

- [**State**](./docs/State.md) - - [`createMemo`](./docs/createMemo.md) — creates a memoized hook. + - [`createMemo`](./docs/createMemo.md) — factory of memoized hooks. - [`useGetSet`](./docs/useGetSet.md) — returns state getter `get()` instead of raw state. - [`useGetSetState`](./docs/useGetSetState.md) — as if [`useGetSet`](./docs/useGetSet.md) and [`useSetState`](./docs/useSetState.md) had a baby. - [`useObservable`](./docs/useObservable.md) — tracks latest value of an `Observable`. diff --git a/docs/createMemo.md b/docs/createMemo.md index 0fc5c1f0ab..9712f7c89e 100644 --- a/docs/createMemo.md +++ b/docs/createMemo.md @@ -1,6 +1,7 @@ # `createMemo` -A factory that returns a React hook, which is memoized for each set of arguments passed to it. +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 @@ -26,3 +27,10 @@ const Demo = () => { ); }; ``` + + +## Reference + +```js +const useMemoFn = createMemo(fn); +```