javascript - PropTypes of specific component? -
i've got simple react component:
import {proptypes} 'react'; import column './column'; export default function columncontainer({children}) { return ( <div classname="cr-column-container">{children}</div> ); } let columntype = proptypes.instanceof(column); columncontainer.proptypes = { children: proptypes.oneoftype([ columntype, proptypes.arrayof(columntype), ]).isrequired, };
which use this:
render() { return ( <columncontainer> <column> *snip* </column> </columncontainer> ); }
but it's failing prop-type validation:
warning: failed prop type: invalid prop `children` supplied `columncontainer`. in columncontainer (created examplecontainer) in examplecontainer
why that? i've used column
s inside of columncontainer
. proptypes.instanceof(column)
not work expect? how supposed specify columncontainer
accept children of type column
?
did digging, came helper function based on josephmartin09's solution:
export function childrenof(...types) { let fieldtype = proptypes.shape({ type: proptypes.oneof(types), }); return proptypes.oneoftype([ fieldtype, proptypes.arrayof(fieldtype), ]); }
usage:
columncontainer.proptypes = { children: childrenof(column).isrequired, };
it's not ideal because doesn't support native dom elements 'div'
, error message worthless, it'll now.
Comments
Post a Comment