-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from streamich/createMemo
Create memo
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters