Last active
July 1, 2017 19:53
-
-
Save j5bot/a5b22bc7a999379ffd736f9749221145 to your computer and use it in GitHub Desktop.
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'; // required to use JSX | |
export const CapitalizedReferenceComponentCollectionExternals = (props) => { | |
// get references to all possible components | |
// that this component might render, | |
// and the collection of dynamic components we need to render, | |
// using destructuring | |
const { components: Components, collection } = props; | |
// A Capitalized reference to reuse | |
let Component; | |
// A reference to the component's props to reuse | |
let componentProps; | |
// A function which returns component.props if it exists, | |
// and otherwise returns props | |
const defaultMapPropsToComponent = function ( | |
{ | |
component = {}, | |
props = {} | |
} = {} | |
) { | |
return component.props || props; | |
}; | |
// render the component collection | |
return( | |
<div>{ | |
collection.map( | |
( component ) => { | |
// Reference the proper component | |
Component = Components[ component.type ]; | |
// Get the props you want to use for this component instance | |
// here we are assuming that you can specify a mapping function | |
// on the component definition, in props, or use the default | |
componentProps = ( | |
component.mapPropsToComponent || | |
props.mapPropsToComponent || | |
defaultMapPropsToComponent | |
)( component, props ); | |
return (<Component { ...componentProps } />); | |
} | |
) | |
}</div> | |
); | |
}; | |
export default CapitalizedReferenceComponentCollectionExternals; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment