Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropTypes: define children component type #1836

Closed
SanderSpies opened this issue Jul 15, 2014 · 11 comments
Closed

PropTypes: define children component type #1836

SanderSpies opened this issue Jul 15, 2014 · 11 comments

Comments

@SanderSpies
Copy link
Contributor

I was looking for something like:

propTypes: {
  children: React.PropTypes.instanceOf(OtherComponent)
}

However this doesn't work of course and couldn't find another way of doing this.

Would be nice if this was supported.

@chenglou
Copy link
Contributor

Used to work. Will no longer work with the upcoming descriptor changes. @sebmarkbage has some ideas.

@sebmarkbage
Copy link
Collaborator

There are two pieces to this. The simple one is when you are expecting a single component. We should have a special propType for this.

propTypes: {
  header: React.PropTypes.componentOf(OtherComponent)
}

That's simple. Someone send a pull-request?

The second part is if you want to accept one of several unknown components that are considered compatible. In XHP in Hack we use something called categories.

The third part is when you want to combine these two types of checks with a set of items. What if you want to allow a union type? What if you want the first item to be of a certain type and the remaining items another type?

@azich has some ideas around this.

@sophiebits
Copy link
Collaborator

Another case is if you want to accept any composite that renders to a <td> (for example), not sure if that's covered by XHP categories.

@stubbornella
Copy link

It would be really cool to be able to verify that the right React component was passed in as a property. The docs are a little confusing, because it looks like you should be validating that the right React Message component is getting passed in:

 // An object that could be one of many types
    optionalUnion: React.PropTypes.oneOfType([
      React.PropTypes.string,
      React.PropTypes.number,
      React.PropTypes.instanceOf(Message)
    ]),

http://facebook.github.io/react/docs/reusable-components.html

@sebmarkbage
Copy link
Collaborator

Yea, the current work around is something like: shape({ type: oneOf([MyComponent]) })

I think we need a broader solution than testing for specific types. Usually you want it to correspond to some specific interface but the actual implementation is flexible, or have a certain set of props defined on it.

@danez
Copy link

danez commented Oct 28, 2015

Is there any news on that?

@jimfb
Copy link
Contributor

jimfb commented Oct 28, 2015

propTypes are mostly in a sort of maintenance mode and we're not really planning to add more to them right now.

Proptypes are being phased out in favor of tools like Flow.

@jimfb jimfb closed this as completed Oct 28, 2015
@sebmarkbage
Copy link
Collaborator

That's simple. Someone send a pull-request?

At least part of this is something we should "maintain".

@sebmarkbage sebmarkbage reopened this Oct 28, 2015
@vovacodes
Copy link

Looks like this could be closed, the explanation is here: #7119

@aweary aweary closed this as completed Dec 11, 2016
@vovacodes
Copy link

In case somebody needs this functionality, I published a module for that https://www.npmjs.com/package/react-element-proptypes

@congwenma
Copy link

Added a pr for this feature
facebook/prop-types#146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants