React原生自定义视图,原生prop没有propType



我试图在react native中实现一个本机视图,但在道具方面遇到了一些问题。

我有一个扩展android.view.View的类CustomView和它的扩展com.facebook.react.uimanager.SimpleViewManagerViewManager

与React的结合是这样完成的:

'use strict';
var { requireNativeComponent, PropTypes } = require('react-native');
var iface = {
    name: 'CustomView',
    propTypes: {
        myProp: PropTypes.string
    },
};
module.exports = requireNativeComponent('CustomView', iface);

当我在React应用程序中使用它时,它会抛出一个错误:

`CustomView` has no propType for native prop `CustomView.renderToHardwareTextureAndroid` of native type `boolean`

这是因为SimpleViewManager定义了一些标准属性,如:

@ReactProp(name = PROP_BACKGROUND_COLOR, defaultInt = Color.TRANSPARENT, customType = "Color")
public void setBackgroundColor(T view, int backgroundColor) {
    view.setBackgroundColor(backgroundColor);
}

我可以通过在iface对象中声明每个属性来修复它。

我不想手动列出所有道具,有没有办法把所有道具都放在我的iface中而不必知道它们
类似于:

var {standardViewProps} = require('react-native');
var iface = {
    name: 'CustomView',
    propTypes: {
        ...standardViewProps,
        myProp: PropTypes.string
    }
}

编辑2017/06/02:

React Native 0.44反对使用View.propTypes。相反,请执行以下操作:

import { ViewPropTypes } from "react-native";
/* later... */
propTypes: {
    ...ViewPropTypes,
    myProp: PropTypes.string
}

上一个答案:

绝对:

var View = React.View;
/* later... */
propTypes: {
    ...View.propTypes,
    myProp: PropTypes.string
}

最新更新