Hacker News new | past | comments | ask | show | jobs | submit login

As a real quick example, purely off the top of my head:

    @connect(state => ({a : state.a})
    class MyComponent extends React.Component {}

    MyComponent.propTypes = {a : PropTypes.string.required, b : PropTypes.number.required}
    MyComponent.defaultProps = {b : 42}
In this example, both the propTypes and defaultProps definitions are being applied to the wrapper component, not the actual "plain" MyComponent class. So, while the required `b` prop might get satisfied almost accidentally from the defaultProps value, the required `a` prop won't exist on the wrapper, as the wrapper itself is extracting that value from the Redux state internally and passing it to the plain MyComponent.

I've definitely seen this pop up as a recurring question that's confused people.




Ah. That has never come up for me because I assign `propTypes` and `defaultProps` within the class definition using class properties. I had assumed you would be assuming I would too because you made mention of class properties in your post. Who would go to the trouble of setting up decorators without also using class properties syntax? Quite a few people, apparently, but the practice of using one and not the other baffles me.


Yeah, I actually wasn't 100% sure whether class properties + a decorator would play together as "expected". Thinking about it, I would guess they must, in which case defining those values as class properties also would be reasonable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: