2017-04-05 70 views
0

我目前正在使用React“组件”(它不显示任何内容,只是充当调度程序)来完成某些一次性请求的反应原生应用程序在JavaScript中。 JS代码很简单:React本地组件未安装

import React from 'react' 
import { AppRegistry, DeviceEventEmitter } from 'react-native' 

class NativeEventDispatcher extends React.Component { 
    constructor() { 
    super() 
    console.log("anyone home?") 
    } 
    componentDidMount() { 
    DeviceEventEmitter.addListener('DoSomething',() => {}) 
    } 
    render() { 
    return null 
    } 
} 

AppRegistry.registerComponent('NativeEventDispatcher',() => NativeEventDispatcher); 

然而对于某些原因,呼吁

val rootView = ReactRootView(context) 
rootView.startReactApplication(reactInstanceManager, "NativeEventDispatcher", null) 
addContentView(rootView, ViewGroup.LayoutParams(0, 0)) 
reactInstanceManager.onHostResume(this, this) 

没有任何反应了。该组件不会挂载,JS文件中的断点不会被执行,构造函数中的日志也不会被打印。几乎就像组件安装失败一样。

有没有人遇到过这个?任何可能的解一直试图调试一天左右,现在没有运气。

回答

0

我不知道第二个东西,但我会写这样的第一个。

import React, { Component } from 'react'; 
import { View, AppRegistry, DeviceEventEmitter } from 'react-native'; 

class NativeEventDispatcher extends Component { 
    constructor(props) { 
     super(props) 
     console.log("anyone home?") 
    } 
    componentDidMount() { 
     DeviceEventEmitter.addListener('DoSomething',() => {}) 
    } 
    render() { 
     return (
      <View></View> 
     ); 
    } 
} 

export default NativeEventDispatcher; 

AppRegistry.registerComponent('NativeEventDispatcher',() => NativeEventDispatcher); 

您应该出口默认

+0

此代码是'index.android.js'因为我们还在校对了这一点,导出类。但是,是的,在一个单独的模块中,我们必须导出它。话虽如此,我仍然不知道将'return null'改为'return '会做任何事情。构造函数甚至没有被调用,所以渲染方法不应该与它有很大关系。 – amarkon