Created
May 1, 2018 21:02
-
-
Save bradharms/b06a714d431cbbbbc862168c05307bac to your computer and use it in GitHub Desktop.
Composable Components with IC
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
window.onload = () => ReactDOM.render(<App />, document.body); | |
class App extends React.Component { | |
render() { | |
const pid = 999; | |
const ProductClosure = makeProductClosure({ | |
ProductThumbnail, | |
ProductEditor | |
}); | |
return <main> | |
<header>Customize Product</header> | |
<ProductClosure productId={pid}>{C => | |
<div> | |
<C.ProductThumbnail /> | |
<C.ProductEditor /> | |
</div> | |
}</ProductClosure> | |
</main> | |
} | |
} | |
const makeProductClosure = types => ({ | |
productId, | |
children | |
}) => children(Object.assign({}, | |
...Object.entries(types).map(([name, Component]) => ({ | |
[name]: ({children, ...props}) => | |
<Component productId={productId} {...props}> | |
{children} | |
</Component> | |
})) | |
)); | |
const ProductThumbnail = ({ productId }) => | |
<div className="product-thumbnail"> | |
<span data-product-id={productId} /> | |
</div>; | |
const ProductEditor = ({ productId }) => | |
<div className="product-editor"> | |
<span data-product-id={productId} /> | |
</div>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment