7

I have a prop that is usually an array of objects, but sometimes (including on the component's first render) the array is empty. I know that using PropTypes.array is frowned upon and I should use PropTypes.arrayOf() instead, but if I use PropTypes.arrayOf(PropTypes.object), there is a failed prop type warning due to the empty state of the array. What is the correct way to type check this prop?

1
  • You can define defaultProps to an empty array Commented Dec 14, 2016 at 2:36

1 Answer 1

9

You shouldn't be getting an error unless you set isRequired on it. That is,

myArray: PropTypes.arrayOf(PropTypes.object).isRequired

This will require an array but not necessarily an object. It's how you would handle exactly the case you mention where initially you may pass an empty array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.