ReactNative中如何使用React的组件?

在RN的工程中想使用一个react写的组件
但是使用后会报错

var React = require('react/react');
var ReactNative = require('react-native');

var ReactNativeView = ReactNative.createClass({
    //...
    render:function(){
        return(
            <View>
                <ReactTestComponent />
            </View>
        );
    }
});

var ReactTestComponent = React.createClass({
  render: function() {
    return (
      <div>
        react组件
      </div>
    );
  }
}); 

报错说div不符合命名规范,我只是想用div标签啊 并不是自定义一个Div

[info][tid:com.facebook.React.JavaScript] {}
[warn][tid:com.facebook.React.JavaScript] 
'Error: Invariant Violation: Expected a component class, got div. 
Each component name should start with an uppercase letter.\n stack: \n  
<unknown>                                
message: Invariant Violation: Expected a component class, got div. Each component name should start with an uppercase letter.'
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views
[error][tid:com.facebook.React.ShadowQueue][RCTBatchedBridge.m:784] Exception thrown while invoking manageChildren on target RCTUIManager with params (
    7,
        (
        0
    ),
        (
        2
    ),
    "<null>",
    "<null>",
    "<null>"
): *** -[__NSArrayM insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]
[error][tid:main][RCTNavigator.m:494] Cannot adjust current top of stack beyond available views

请问如何正确的在ReactNative中使用React的组件?

阅读 7.1k
2 个回答

环境不同
浏览器环境中,以 ReactDOM (也就是一般的react)渲染,是有<div/>标签的。
但在手机客户端环境
以 ReactNative 渲染,没有<div/>标签,但对应的,可以使用<View />之类的标签。

所以,不同环境下的 react 组件,大部分是无法无缝迁移的,需要自己进行封装

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题