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

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -