javascript - Specifying a prop to be a specific Component in React -
i'm writing component, , wan't 1 of props alway instances of spesific component
class component extends react.component { static proptypes: { children: /*only childcomponents allowed*/ } } class childcomponent extends react.component {} <component><div /></component> // should fail <component><div /><childcomponent></component> // should fail <component><childcomponent /></component> // should succedd <component><childcomponent /><childcomponent /></component> // should succedd
children: react.proptypes.instanceof(childcomponent) look here - https://facebook.github.io/react/docs/reusable-components.html.
update instanceof not working in case, try alternate approach -
in proptypes, allow node -
children: react.proptypes.node then in render method -
react.children.map(children, (child, index) => { if (!react.isvalidelement(child) || !child.type || child.type.displayname !== 'childcomponent'){ // exterminate! exterminate! } }
Comments
Post a Comment