在 React Native 中验证样式 属性 类型

Validate Style property type in React Native

我有一个采用 属性 的组件,其中包含子组件的符号。我想确保 propTypes 正确验证它的类型。我可以从 React Native 代码中得知它有一个提供此功能的 ViewStylePropTypes 模块,但是我似乎找不到 where/if 它已公开。

我想知道的是,在不重新发明轮子的情况下验证这一点的正确方法是什么?

要对 PropTypes 实施样式限制,只需使用以下内容,具体取决于您渲染的组件类型:

MyComponent.propTypes = {
  /**
  * Style to be applied to the containing <View>
  */
  buttonStyle: View.propTypes.style,
  /**
  * Style to be applied to the inner <Text>
  */
  textStyle: Text.propTypes.style
}

例如,当 bordertextStyle 属性.

中定义时,Text.propTypes.style 将显示黄框警告

注意: 这也将导致在具有无效样式属性的组件内渲染 Text 时出现常规 Failed prop type supplied to Text... 警告。 propTypes 验证允许您的自定义组件同时验证它,为您提供更好的粒度。