简体   繁体   中英

React Native component not mounting

I'm currently working on a react-native application where we're using a React "component" (it doesn't display anything, just acts as a dispatcher) to complete some one-off requests in JavaScript. The JS code is very simple:

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);

For some reason however, after calling

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

nothing happens. The component doesn't mount, no breakpoints in the JS file get executed, the log in the constructor never prints. Almost as though the component mount is failing silently.

Has anyone encountered this before? Any potential solutions? Been trying to debug this for a day or so now with no luck.

I don't know about second stuff but I would write first one like this.

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);

You should export your class with export default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM