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

React PropType validations #45

Open
madki opened this issue Apr 10, 2017 · 1 comment
Open

React PropType validations #45

madki opened this issue Apr 10, 2017 · 1 comment

Comments

@madki
Copy link

madki commented Apr 10, 2017

Hi,

I'm using typed-immutables in a react project. I have a type called Card and it's passed as a prop to CardComponent. Similarly there's CardList and CardListComponent

const Card = Record({
  id: String,
  title: String
}, 'Card')

export const CardList = List(Card, 'CardList') 

export default Card

How would I validate them with react PropTypes? Are there any methods that I can use, that go as follows

const CardComponent = (card) => {
  return (
    <div>${card.title}</div>
  )
}

CardComponent.propTypes = {
  card: Card.isRequired // or Card.isOptional
}

How do I go about doing those validations now? Validations for CardList seem to be harder. What would be a good way to implement these validations?

@davecoates
Copy link
Member

I think you should just be able to use PropTypes.instanceOf:

CardComponent.propTypes = {
   card: PropTypes.instanceOf(Card).isRequired,
}

same for list

CardListComponent.propTypes = {
   cards: PropTypes.instanceOf(CardList).isRequired,
}

Does that work?

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

2 participants