You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a component and it can take a prop of 'src' or children, but never both. I was unable to find a proptype supplied in this library to cover this.
@ljharb was helping me compose a prop-type for this and while it does seem to disallow the other prop if one is already provided, it doesn't also require that one exist.
I also ran into this problem, and ended up using the following solution:
constexclusivePropTypes={foo: fooValidator,bar: barValidator,baz: bazValidator}constexclusiveProps=Object.keys(exclusivePropTypes)Component.propTypes={
...Object.fromEntries(exclusiveProps.map(exclusiveProp=>[exclusiveProp,AirbnbPropTypes.and([exclusivePropTypes[exclusiveProp],(props,propName,componentName, ...rest)=>{constpropList=exclusiveProps.join(', ')constexclusivePropCount=Object.keys(props).filter(prop=>props[prop]!=null).reduce((count,prop)=>(count+(exclusivePropTypes[prop] ? 1 : 0)),0)if(exclusivePropCount>1){returnnewError(`A ${componentName} cannot have more than one of these props: ${propList}`)}if(exclusivePropCount<1){returnnewError(`A ${componentName} must have at least one of these props: ${propList}`)}}])]))}
I have a component and it can take a prop of 'src' or children, but never both. I was unable to find a proptype supplied in this library to cover this.
@ljharb was helping me compose a prop-type for this and while it does seem to disallow the other prop if one is already provided, it doesn't also require that one exist.
here is the proposed prop-type:
static propTypes = {
src: disallowedIf(string, "children", any.isRequired),
children: disallowedIf(
oneOfType([arrayOf(node), node]),
"src",
any.isRequired
)
};
I am happy to contribute a solution for a mutulallyExclusivelyRequired or something similar if needs be.
Thanks!
The text was updated successfully, but these errors were encountered: