对象不是有效的React子对象(关于Firebase)

时间:2018-06-26 10:07:31

标签: react-native

我创建了一个新的本机应用程序。我只通过npm安装firebase并将其导入。我什么也没做,但这就是我运行该应用程序时得到的。

This is the exact error that i get.

这是代码。它是本机已经创建的默认App.js。我只进口firebase。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, {
  Component
} from 'react';
import firebase from 'firebase';
import {
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

export default class App extends Component {
  render() {
    return ( <
      View style = {
        styles.container
      } >
      <
      Text style = {
        styles.welcome
      } >
      Welcome to React Native!
      <
      /Text> <
      Text style = {
        styles.instructions
      } >
      To get started, edit App.js <
      /Text> <
      Text style = {
        styles.instructions
      } > {
        instructions
      } <
      /Text> < /
      View >
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

1 个答案:

答案 0 :(得分:2)

从App.js中删除import firebase from 'firebase';并按如下所示更改类。

export default class App extends Component {
  initializeFirebase() {
    const firebase = require("firebase");
      firebase.initializeApp({
        apiKey: '<your_key>',
        authDomain: '<your_domain>',
        databaseURL: '<your_url>',
        projectId: '<your_projectID>',
        storageBucket: '<your_bucket>',
        messagingSenderId: '<your_id>'
      });
  }

  componentWillMount() {
    this.initializeFirebase();
  }

    ...

}