Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native custom view, no propType for native prop

I'm trying to implement a native view in react-native, but having some problems with the props.

I have a class CustomView extending the android.view.View and its ViewManager extending com.facebook.react.uimanager.SimpleViewManager.

The binding with React is done in this way:

'use strict';

var { requireNativeComponent, PropTypes } = require('react-native');

var iface = {
    name: 'CustomView',
    propTypes: {
        myProp: PropTypes.string
    },
};
module.exports = requireNativeComponent('CustomView', iface);

When I use it in my React app, it throws an error:

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

This is because SimpleViewManager has defined some standard property like:

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

I can fix it just declaring each property in my iface object.

I don't want to list manually all the props, is there a way to put all that props in my iface without have to know them?
Something like:

var {standardViewProps} = require('react-native');

var iface = {
    name: 'CustomView',
    propTypes: {
        ...standardViewProps,
        myProp: PropTypes.string
    }
}
like image 895
rascio Avatar asked Dec 10 '15 15:12

rascio


1 Answers

EDIT 2017/06/02:

React Native 0.44 has deprecated the use of View.propTypes. Instead, do the following:

import { ViewPropTypes } from "react-native";

/* later... */
propTypes: {
    ...ViewPropTypes,
    myProp: PropTypes.string
}

PREVIOUS ANSWER:

Absolutely:

var View = React.View;

/* later... */
propTypes: {
    ...View.propTypes,
    myProp: PropTypes.string
}
like image 196
John Shammas Avatar answered Sep 19 '22 12:09

John Shammas