Last active
December 18, 2018 20:51
-
-
Save JayKan/08eaffe1860e8cf10d596c6644a67e92 to your computer and use it in GitHub Desktop.
[experimental] Compose render props
This file contains hidden or 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
| import { cloneElement } from 'react'; | |
| import PropTypes from 'prop-types'; | |
| function Compose(props) { | |
| return function renderRecursive(props.children, props.components, results = []) { | |
| if (!remaining[0]) { | |
| render(results); | |
| } | |
| function nextRender(value) { | |
| return renderRecursive(render, remaining.slice(), results.concat[value]); | |
| } | |
| return typeof remaining[0] === 'function' | |
| ? remaining[0]({ result, render: nextRender }) | |
| : cloneElement(remaining[0], children: nextRender }); | |
| } | |
| } | |
| Compose.propTypes = { | |
| children: PropTypes.func.isRequired, | |
| components: PropTypes.arrayOf( | |
| PropTypes.oneOfType([PropTypes.element, PropTypes.func]), | |
| ).isRequired, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment