React native - 对象无效作为React子对象(找到:具有键{$$ typeof,type,key,ref,props,_owner,_store}的对象)

scr*_*azz 26 javascript android reactjs react-native

我是React Native的新手,我在下面引用了一个错误:

对象无效作为React子对象(找到:具有键{$$ typeof,type,key,ref,props,_owner,_store}的对象).如果您要渲染子集合,请使用数组.

这是我的整个代码包含在组件文件中,除了样式:

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

class LoginForm extends Component {

    state = { email: '', password: '', error: '', loading: false };

    onButtonPress(){
        const email = this.state.email;
        const password = this.state.password;

        this.setState({error: '', loading: true});

        firebase.auth().signInWithEmailAndPassword(email, password)
            .then(this.onLoginSuccess.bind(this))
            .catch(() => {
                firebase.auth().createUserWithEmailAndPassword(email, password)
                .then(this.onLoginSuccess.bind(this))
                .catch(this.onLoginFail.bind(this));
            });
    }

    onLoginSuccess(){
        this.setState({email: '', password: '', error: '', loading: false});
    }

    onLoginFail(){
        this.setState({error: 'Nie udalo sie utworzyc konta.', loading: false});
    }

    render(){
        return(
            <View style={styles.container}>
                <View style={styles.imageContainer}>
                    <Image 
                        style={styles.image}
                        source={require('../images/loginIcon.png')}
                    />
                </View>
                <View style={styles.formContainer}>
                    <TextInput
                        style={styles.input}
                        placeholder="Email..."
                        placeholderTextColor='rgba(255,255,255,0.9)'
                        underlineColorAndroid='rgba(0,0,0,0)'
                        onChangeText={(email) => this.setState({email: email})}
                        value={this.state.email}
                        autoCorrect={false}
                    />
                    <TextInput
                        style={styles.input}
                        placeholder="Has?o..."
                        placeholderTextColor='rgba(255,255,255,0.9)'
                        underlineColorAndroid='rgba(0,0,0,0)'
                        autoCorrect={false}
                        onChangeText={(password) => this.setState({password: password})}
                        value={this.state.password}
                        secureTextEntry
                    />
                    <TouchableOpacity style={styles.buttonContainer}>
                        <Text style={styles.button}>
                            Zaloguj si?
                        </Text>
                    </TouchableOpacity>
                    <Text style={styles.error}>
                        {this.state.error}
                    </Text>
                </View>
            </View>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

我很困惑如何解决这个问题.提前致谢.

Gro*_*Src 41

我今天遇到了这个问题.我在5.0.3和5.0.4之间的源代码上运行了差异,发现导出已经改变.我还发现,如果我将import语句更改为以下版本,它将与最新版本(5.3.0)一起使用:

import firebase from '@firebase/app'
import '@firebase/auth'
Run Code Online (Sandbox Code Playgroud)

这样做可以让你避免require代码中间,这是首选的imho.

  • 为此,您需要导入您正在使用的Firebase的所有部件.我没有使用Auth,但使用的是存储和数据库.所以我不得不添加:import"@ firebase/storage"; 导入"@ firebase/database"; (3认同)

小智 33

试试这个:

从App.js中删除firebase import语句:

import firebase from 'firebase'
Run Code Online (Sandbox Code Playgroud)

在Firebase初始化时创建一个常量:

initializeFirebase() {
  const firebase = require("firebase");

  // Initialize Firebase
  var config = {
  ...
  };
  firebase.initializeApp(config);

  //inicializando o firestore
  const firestore = require("firebase/firestore");
  db = firebase.firestore();
  db.settings({ timestampsInSnapshots: true });
}

componentWillMount() {
  this.initializeFirebase();
...
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这个解决方案非常有效!

  • 这对我有用,但不要错过@ GrokSrc的解决方案,下面解释了如何仍然使用'import` - >`import firebase from'@ firebase/app'`和`import'@ firebase/auth'` (2认同)

小智 15

这是firebase版本5.0.6的问题,而降级到版本将解决此问题.例如试试这个

$ npm install --save firebase@5.0.4

如果版本5.0.4也不适用于您而不是尝试版本4.9.1,因为这是我正在使用的,它为我解决了这个问题

$npm install --save firebase@4.9.1

  • 这对我有用.我开始使用4.9.1版本,问题就消失了. (3认同)

小智 7

尝试将import语句更改为:

import firebase from '@firebase/app';
Run Code Online (Sandbox Code Playgroud)