Closed dkreft closed 3 years ago
If foo
is required for B
, then it's absolutely required for A
in your example. In other words, omitting it on A will be a bug when it gets to B and isn't present, so it should be required for both.
Do you have another example?
@ljharb, I think you may have missed A.defaultProps
in the third code block:
In that case, it is optional for A
by virtue of the defaultProps
, but of course, prop-types
still recognizes it as a mandatory property in A
because there's no way to override the isRequired
...which is the issue that I'm seeking to resolve.
I could still pass foo
as null
and it would break, because it should, in fact, be required on A.
I feel like maybe we're speaking past each other.
I'm trying to figure out a way to make the param mandatory for B
but optional for A
, but it sounds like you're talking about the status quo.
If you have a recommendation for how to achieve this, I'm all ears.
This is how:
A.propTypes = {
foo: B.propTypes.foo, // required
};
A.defaultProps = {
foo: 'some default'
};
This way, foo
is required, but because it is also defaulted, the user doesn't have to pass it - but if they pass null
it's invalid, which is correct.
Hrmm. Well, then...I suppose I'll just crawl back under the rock out from which I came.
When passing props from one component to another, I like to have a single source of truth for my propTypes--the component that actually uses the prop:
In this case, the
foo
property onA
is mandatory...which is great. Well, that is until I want to makefoo
optional onA
because I'm declaring a default value for it:What I'd like to be able to do is override the
isRequired
flag, perhaps like this:If there's some other reasonable way around this problem, I'm all ears.