По крайней мере, один требуемый опора в реактиве
Мне нужно сделать хотя бы один из необходимых реквизитов:
MyComponent.propTypes = {
data: PropTypes.object,
url: PropTypes.string
};
Итак, в приведенном выше примере необходимо указать либо data
, либо url
prop. Вариант использования здесь заключается в том, что пользователь может либо предоставить data
, либо url
. Если предоставляется url
, то компонент будет извлекать data
.
Бонусный вопрос: как я могу сделать хотя бы одну опору против одного из реквизитов?
Ответы
Ответ 1
PropTypes фактически может принимать пользовательскую функцию в качестве аргумента, чтобы вы могли сделать что-то вроде этого:
MyComponent.propTypes = {
data: (props, propName, componentName) => {
if (!props.data && !props.url) {
return new Error(`One of props 'data' or 'url' was not specified in '${componentName}'.`);
}
},
url: (props, propName, componentName) => {
if (!props.data && !props.url) {
return new Error(`One of props 'url' or 'data' was not specified in '${componentName}'.`);
}
}
}
который позволяет отправлять сообщения об ошибках клиента. Вы можете возвращать только null или Error при использовании этого метода
Подробнее об этом можно узнать здесь.
https://facebook.github.io/react/docs/typechecking-with-proptypes.html#react.proptypes
из интерактивных документов:
// You can also specify a custom validator. It should return an Error
// object if the validation fails. Don't `console.warn` or throw, as this
// won't work inside `oneOfType`.
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
},
Ответ 2
Более краткий вариант решения @finalfreq:
const requiredPropsCheck = (props, propName, componentName) => {
if (!props.data && !props.url) {
return new Error(`One of 'data' or 'url' is required by '${componentName}' component.`)
}
}
Markdown.propTypes = {
data: requiredPropsCheck,
url: requiredPropsCheck,
}
Ответ 3
Добавление в верхней части finalfreq ответ, касающийся kousha комментария к нему.
У меня был компонент кнопки, который требовал либо значок, либо заголовок. Убедитесь, что хотя бы один из них есть, как в приведенном выше ответе, после этой проверки его тип может быть проверен следующим образом:
Button.propTypes = {
icon: (props, propName, componentName) => {
if (!props.icon && !props.title) {
return new Error('One of props 'icon' or 'title' was not specified in '${componentName}'.')
}
if (props.icon) {
PropTypes.checkPropTypes({
icon: PropTypes.string, // or any other PropTypes you want
},
{ icon: props.icon },
'prop',
'PrimaryButtonWithoutTheme')
}
return null
}
title: // same process
}
Для получения дополнительной информации о PropTypes.checkPropTypes
читайте здесь
Ответ 4
Я написал этот помощник, чтобы решить ту же проблему в многократном использовании. Вы используете его как функцию propType:
MyComponent.propTypes = {
normalProp: PropType.string.isRequired,
foo: requireOneOf({
foo: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]),
bar: PropTypes.string,
}, true),
};
и в этом примере это гарантирует, что один из foo или bar находится в подпорках MyComponent. Если вы пропустите второй аргумент, он обеспечит пропуск только одного из foo или bar.
/**
* Takes a propTypes object ensuring that at least one of the passed types
* exists on the component.
*
* Usage:
*
* MyComponent.propTypes = {
* normalProp: PropType.string.isRequired,
*
* foo: requireOneOf({
* foo: PropTypes.oneOfType([
* PropTypes.string,
* PropTypes.number
* ]),
* bar: PropTypes.string,
* }, true),
* };
*
* @param requiredProps object
* @param allowMultiple bool = false If true multiple props may be
* passed to the component
* @return {Function(props, propName, componentName, location)}
*/
export const requireOneOf = (requiredProps, allowMultiple = false) => {
return (props, propName, componentName, location) => {
let found = false;
for (let requiredPropName in requiredProps) {
if (requiredProps.hasOwnProperty(requiredPropName)) {
// Does the prop exist?
if (props[requiredPropName] !== undefined) {
if (!allowMultiple && found) {
return new Error(
'Props ${found} and ${requiredPropName} were both passed to ${componentName}'
);
}
const singleRequiredProp = {};
singleRequiredProp[requiredPropName] = requiredProps[requiredPropName];
const singleProp = {};
singleProp[requiredPropName] = props[requiredPropName];
// Does the prop match the type?
try {
PropTypes.checkPropTypes(singleRequiredProp, singleProp, location, componentName);
} catch (e) {
return e;
}
found = requiredPropName;
}
}
}
if (found === false) {
const propNames = Object.keys(requiredProps).join('", "');
return new Error(
'One of "${propNames}" is required in ${componentName}'
);
}
};
};
Ответ 5
function requireALeastOne(checkProps) {
return function(props, propName, compName) {
const requirePropNames = Object.keys(checkProps);
const found = requirePropNames.find((propRequired) => props[propRequired]);
try {
if (!found) {
throw new Error(
'One of ${requirePropNames.join(',')} is required by '${compName}' component.',
);
}
PropTypes.checkPropTypes(checkProps, props, propName, compName);
} catch (e) {
return e;
}
return null;
};
}
const requireALeast = requireALeastOne({
prop1: PropTypes.string,
prop2: PropTypes.number
});
Comp.propTypes = {
prop1: requireALeast,
prop2: requireALeast
};