-
-
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.
- Loading branch information
Showing
5 changed files
with
94 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,31 @@ | ||
# `useRenderProp` | ||
|
||
Extracts a value from render-prop or FaCC component. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useRenderProp} from 'react-use'; | ||
|
||
const FaCC = ({children}) => { | ||
return children('VALUE-FaCC'); | ||
}; | ||
const RenderProp = ({render}) => { | ||
return render('VALUE-RenderProp'); | ||
}; | ||
|
||
const Demo = () => { | ||
const [fragment1, [value1]] = useRenderProp(<FaCC />); | ||
const [fragment2, [value2]] = useRenderProp(<RenderProp />); | ||
|
||
return ( | ||
<> | ||
{fragment1} | ||
{fragment2} | ||
<div>FaCC: {value1}</div> | ||
<div>Render prop: {value2}</div> | ||
</> | ||
); | ||
}; | ||
``` |
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,31 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useRenderProp} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const FaCC = ({children}) => { | ||
return children('VALUE-FaCC'); | ||
}; | ||
const RenderProp = ({render}) => { | ||
return render('VALUE-RenderProp'); | ||
}; | ||
|
||
const Demo = () => { | ||
const [fragment1, [value1]] = useRenderProp(<FaCC />); | ||
const [fragment2, [value2]] = useRenderProp(<RenderProp />); | ||
|
||
return ( | ||
<> | ||
{fragment1} | ||
{fragment2} | ||
<div>FaCC: {value1}</div> | ||
<div>Render prop: {value2}</div> | ||
</> | ||
); | ||
}; | ||
|
||
storiesOf('useRenderProp', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useRenderProp.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
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,29 @@ | ||
import * as React from 'react'; | ||
import {useState, useCallback} from './react'; | ||
import createMemo from './createMemo'; | ||
|
||
const useRenderProp = (element: React.ReactElement<any>): [React.ReactElement<any>, any[]] => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!React.isValidElement(element)) { | ||
throw new TypeError( | ||
'useRenderProp element to be a valid React element ' + | ||
'such as <MyRenderProp />.' | ||
); | ||
} | ||
} | ||
|
||
const [state, setState] = useState<any[]>([]); | ||
const useSetState = createMemo((...args) => setState(args)); | ||
const render = useCallback((...args) => { | ||
useSetState(...args); | ||
return null; | ||
}, []); | ||
const cloned = React.cloneElement(element, { | ||
render, | ||
children: render, | ||
}); | ||
|
||
return [cloned, state]; | ||
}; | ||
|
||
export default useRenderProp; |