I am creating a component that I want the element type to be configurable.
const Col = ({ containerElement, children }) => {
return (
<containerElement>
{children}
</containerElement>
);
};
Col.defaultProps = {
containerElement: 'div'
};
So container element could either be as in the defaultProps above or it could be a component.
<Col containerElement={<MyComponent} />
I can't get the propTypes to validate, I have tried this:
Col.propTypes = {
className: PropTypes.string,
containerElement: PropTypes.oneOf([
PropTypes.string,
PropTypes.element
]),
But it does not validate correctly.
Warning: Failed propType: Invalid prop
componentClassof valuedivsupplied toCol,
<containerElement>anyway.<containerElement>is syntactic sugar forReact.createElement('containerElement', ...). Even if it is a string,'containerElement'will be passed toReact.createElement, not the variablecontainerElement. Before worrying about the validation error I would make sure the code itself produces the correct result (i.e. works correctly). - Felix Klingconst ContainerElement = containerElement.inner. See this question: stackoverflow.com/a/39655113/504018 - patcon