2
votes

I am doing proptype check in my React.js code like this

const { a, b ,c } = this.props

where a, b, c are my objects or response coming from API

and in my propTypes check my code look's like

ComponentName.propTypes = {
a: PropTypes.objectOf,
b: PropTypes.objectOf,
c: PropTypes.objectOf,
};

in Default PropTypes assigning it like this

ComponentName.defaultProps = {
a: {
key1: '',
key2: '',
},
b: {
key3: '',
key4: '',
},
},

But when i am compiling my code it is giving me this error

type specification of prop a is invalid; the type checker function must return null or an Error but returned a function. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument)

Please can someone help in removing this warning from the console what can be the workaround

1
change to a: PropTypes.objectOf(PropTypes.string), b: PropTypes.objectOf(PropTypes.string), ... - Sahil Raj Thapa
have done that it gives a warning of Invalid prop a.key of type number supplied to ComponentName, expected string - Mayank Purohit
if you are passing number as props then use a: PropTypes.objectOf(PropTypes.number), ... - Sahil Raj Thapa
actually that particular object have multiple data types as its key value pair's so my question is can i pass multiple PropTypes.dataType as objectOf parameter - Mayank Purohit

1 Answers

4
votes

Use PropTypes.any for value of any data type

ComponentName.propTypes = {
 a: PropTypes.objectOf(PropTypes.any),
 b: PropTypes.objectOf(PropTypes.any),
 c: PropTypes.objectOf(PropTypes.any),
};