I established my PropTypes on App component for all props that are being passed to children.
class App extends Component {
constructor( props ) {
super( props );
this.state = {
anyProps : undefined',
};
}
propsValues = {
anyProp: [ 'undefined', 'neutral', 'evil', 'good' ],
};
(...)
I can receive PropTypes on child stateless components, and use its value to validate other props, like this:
const SFComponent = ({
anyProp,
propsValues,
}) => {
SFComponent.propTypes = {
anyProp : PropTypes.oneOf( propsValues.anyProp ).isRequired,
propsValues : PropTypes.objectOf( PropTypes.arrayOf( PropTypes.string )),
};
(...)
But when I try to do the same with a child stateful component it just doesn't work. I can't use the same sintaxis and props are still not received when valitdating types.
class StatefulComponent extends React.Component {
constructor( props ) {
super( props );
this.state = {
anyAttribute : true,
anotherAttribute : null,
};
}
static propTypes = {
anyProp : PropTypes.oneOf( this.props.propsValues.anyProp ).isRequired,
propsValues : PropTypes.objectOf( PropTypes.arrayOf( PropTypes.string )),
(...)
The above snippet throws an error stating this is undefined. I've also tried other possibilities but none works.
So, any ideas on how can I achieve the same result that I achieve in stateless components, being able to use props.propsValues as a base validator for all my props on child stateful components?
Thanks!