Search code examples
javascriptreactjsreact-proptypes

How to update this deprecated React.PropTypes code


Below is the current error I'm getting:

enter image description here

Here is my current PropTypes code, I need to check if the objects inside of the Array coming in have the following properties (id and name).

React.PropTypes.arrayOf(
    React.PropTypes.shape({
        id: React.PropTypes.string.isRequired,
        name: React.PropTypes.string.isRequired
    })
).isRequired

Reading here https://github.com/tomchentw/react-google-maps/issues/463 apparently React.PropTypes is going to be deprecated.

However how would you check the types inside of an Object that is in an Array coming in via the Props with just this logic:

AssetsTable.propTypes = {
    asset: PropTypes.object.isRequired,
    price_usd: PropTypes.string.isRequired
};

Basically, the Array coming in, has objects which look like this:

enter image description here


Solution

  • Prop-types supports shape just like React.PropTypes

    From the React Docs:

      // An object taking on a particular shape
      optionalObjectWithShape: PropTypes.shape({
        color: PropTypes.string,
        fontSize: PropTypes.number
      }),