【问题标题】:What is the prop type for StyleSheet validationStyleSheet 验证的道具类型是什么
【发布时间】:2018-09-03 10:56:39
【问题描述】:

我正在为我们的 react-native 项目编写组件

我从 prop-types 得到这个错误

“警告:失败的道具类型:提供给ImagePickernumber类型的无效道具containerStyle,应为object。”

简化示例:

common-styles.js

    export const defaultStyle = StyleSheet.create({
      container: {
        color: 'red',
        borderWidth: 2
      }
    });

图像选择器.js

    ImagePicker.propTypes = {
      containerStyle: PropTypes.shape(),
    };

app.js

    <ImagePicker containerStyle={styles.defaultStyle} />

传递样式表变量的 PropType 是什么?

谢谢

【问题讨论】:

  • 在哪里定义stylescontainerStyle={styles.defaultStyle})?
  • 以下是一些可能的解决方案。 stackoverflow.com/questions/34626298/…
  • @acdcjunior 你好,如前所述,它来自 common-syles.js
  • @Susth,谢谢尝试,虽然如果我使用数组&lt;ImagePicker containerStyle={[form_input.pads, { borderWidth: 1 }]} /&gt; 会出错,但我会再次检查,看看是否可以解决数组问题

标签: react-native react-proptypes


【解决方案1】:
import { ViewPropTypes } from 'react-native';
ImagePicker.propTypes = {
  containerStyle: ViewPropTypes.style,
};

参考:facebook/react-native /Libraries/Components/View/ViewPropTypes.js

【讨论】:

    【解决方案2】:

    number 类型似乎是有效的,因为它的实际样式保存在聚合样式对象中。仅传递查找 ID

    StyleSheet documentation here

    正确的 PropTypes 应该是

        ImagePicker.propTypes = {
          containerStyle: PropTypes.number,
        };
    

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2022-01-15
      • 2021-01-27
      • 2023-01-01
      • 1970-01-01
      相关资源
      最近更新 更多