admin管理员组文章数量:1418706
I've got this simple React ponent:
import {PropTypes} from 'react';
import Column from './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 I use like this:
render() {
return (
<ColumnContainer>
<Column>
*snip*
</Column>
</ColumnContainer>
);
}
But it's failing prop-type validation:
Warning: Failed prop type: Invalid prop `children` supplied to `ColumnContainer`.
in ColumnContainer (created by ExampleContainer)
in ExampleContainer
Why is that? I've only used Column
s inside of the ColumnContainer
. Does PropTypes.instanceOf(Column)
not work like I expect? How am I supposed to specify that ColumnContainer
will only accept children of type Column
?
I've got this simple React ponent:
import {PropTypes} from 'react';
import Column from './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 I use like this:
render() {
return (
<ColumnContainer>
<Column>
*snip*
</Column>
</ColumnContainer>
);
}
But it's failing prop-type validation:
Warning: Failed prop type: Invalid prop `children` supplied to `ColumnContainer`.
in ColumnContainer (created by ExampleContainer)
in ExampleContainer
Why is that? I've only used Column
s inside of the ColumnContainer
. Does PropTypes.instanceOf(Column)
not work like I expect? How am I supposed to specify that ColumnContainer
will only accept children of type Column
?
- check this out: github./facebook/react/issues/2979 – james emanon Commented Sep 12, 2016 at 19:30
- @jamesemanon Just found that. None of the solutions presented are ideal though. – mpen Commented Sep 12, 2016 at 19:41
2 Answers
Reset to default 5Did some digging, came up with this 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 it doesn't support native DOM elements like 'div'
and the error message is worthless, but it'll do for now.
I overhauled childrenOf
with support for react-hot-loader 4.x:
import { areComponentsEqual } from 'react-hot-loader';
export function childrenOf(...types) {
return requirable((props, propName, ponentName, location, propFullName) => {
const ponent = props[propName];
if(!location) {
location = 'prop';
}
if(!propFullName) {
propFullName = propName;
}
const check = c => types.some(type => areComponentsEqual(type,c.type));
const valid = Array.isArray(ponent) ? ponent.every(check) : check(ponent);
if(!valid) {
return new Error(
`Invalid ${location} \`${propFullName}\` supplied to \`${ponentName}\`. Every element must be a <${types.map(t => getDisplayName(t)).join('|')}>.`
);
}
return null;
});
}
function requirable(predicate) {
const propType = (props, propName, ...rest) => {
// don't do any validation if empty
if(props[propName] === undefined) {
return null;
}
return predicate(props, propName, ...rest);
};
propType.isRequired = (props, propName, ponentName, ...rest) => {
// warn if empty
if(props[propName] === undefined) {
return new Error(`Required prop \`${propName}\` was not specified in \`${ponentName}\`.`);
}
return predicate(props, propName, ponentName, ...rest);
};
return propType;
}
Usage example:
export function TBody({children, ...attrs}) {
return <tbody {...attrs}>{children}</tbody>;
}
TBody.propTypes = {
children: WxTypes.childrenOf(TRow),
};
本文标签: javascriptPropTypes of specific componentStack Overflow
版权声明:本文标题:javascript - PropTypes of specific component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745242104a2649390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论