我试图在react native中实现原生视图,但在props 方面存在一些问题.

我有一个CustomView班延伸android.view.View班,ViewManager班延伸com.facebook.react.uimanager.SimpleViewManager班.

通过以下方式与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对象中声明每个属性就可以修复它.

我不想手动列出所有props ,有没有办法在我的iface中放置所有props 而不必知道它们

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
}

React-native相关问答推荐

Appcenter构建失败错误号::EEXIST -文件存在于syserr_fail2_in

出现错误:使用 process(css).then(cb) 来使用异步插件

在 React Native 中从 Modal 打开 Modal

React Native Expo - 不再支持 Node.js 版本 11.13.0

React Native 获取视图相对于屏幕的绝对位置

api.get(...).then(...).catch(...).finally 不是函数

React-Native iOS - 如何通过按下按钮从 React-Native 视图导航到非 React-Native 视图(本机 iOS 视图控制器)?

redux saga 的delay延迟功能不起作用

React Native - SectionList numColumns support

如何渲染多行文本组件,行间有白色间隙

如何在 React Native 中按住以 Select 文本

如果我有 AngularJS 基础知识,怎么学习Thinking in ReactJS呢?

如何在 React Native 上获取设备的 ip?

在Android上更改高度时react-native TextInput显示错误

如何在 react-native 中从 AsyncStorage 中删除元素

未计算文件的 React-Native Bundle Error 错误 SHA-1

Unexpected token on "export default const"

如何使 react-native Picker 停留在新 Select 的选项?

在 React-native 上加载图像时出错:Unexpected character

使用 React Native 运行 Firebase 3.0 时出错