React propTypes: objectOf vs shape?

前端 未结 2 435
一整个雨季
一整个雨季 2020-12-04 17:26

What\'s the difference between PropTypes.objectOf and PropTypes.shape? In the docs:

// An object with property values of a certain         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 18:02

    PropTypes.objectOf is used when describing an object whose properties are all the same type.

    const objectOfProp = {
      latitude: 37.331706,
      longitude: -122.030783
    }
    
    // PropTypes.objectOf(PropTypes.number)
    

    PropTypes.shape is used when describing an object whose keys are known ahead of time, and may represent different types.

    const shapeProp = {
      name: 'Jane',
      age: 25
    }
    
    // PropTypes.shape({ name: PropTypes.string, age: PropTypes.number })
    

提交回复
热议问题