简体   繁体   English

在 React Native 上插入 Firebase

[英]Insert Firebase on React Native

I am new on React Native, and learn to create a simple CRUD App.我是 React Native 的新手,正在学习创建一个简单的 CRUD 应用程序。 I am using firebase as my storage, I follow a tutorial on this site ` but when I try on insert statement my react app only showing slow preloader when I hit insert button我正在使用 firebase 作为我的存储,我按照这个网站上的教程`但是当我尝试插入语句时,当我点击插入按钮时,我的反应应用程序只显示缓慢的预加载器

Here is my code firebaseDb.js :这是我的代码firebaseDb.js

import * as firebase from 'firebase';
import firestore from 'firebase/firestore'

    const firebaseConfig = {
      apiKey: "MY KEY",
      authDomain: "MY DOMAIN",
      databaseURL: "MY DB URL",
      projectId: "MY PROJECT ID",
      storageBucket: "STORAGE BUCKET",
      messagingSenderId: "SENDER ID",
      appId: "APP ID",
      measurementId: "xxxxxxxxxxxxxxxxxxxxxxx"
    };
  firebase.database.enableLogging(true);
  firebase.initializeApp(firebaseConfig);
    firebase.firestore();

export default firebase;

and here is my source code on AddTodo.js file:这是我在 AddTodo.js 文件上的源代码:

class AddTodo extends Component {

  constructor() {
    super();
    this.dbRef = firebase.firestore().collection('todo');
    this.state = {
      task: '',
      isLoading: false
    };
  }

  inputValueUpdate = (val, prop) => {
    const state = this.state;
    state[prop] = val;
    this.setState(state);
  }

  storeTodo(){
    if(this.state.task === ''){
      alert('task is required !')
    }
    else {
      console.log(this.state.task);
      this.setState({
          isLoading: true,
      });      
      this.dbRef.add({
        task: this.state.task,
      }).then((res) => {
        console.error(res);
          this.setState({
            task: '',
            isLoading: false,
          });
          this.props.navigation.navigate('TodoList')
      })
      .catch((err) => {
        console.error("Error found: ", err);
        this.setState({
          isLoading: false,
        });
      });
    }
  }

  render() {
    if(this.state.isLoading){
      return(
        <View style={styles.preloader}>
          <ActivityIndicator size="large" color="#9E9E9E"/>
        </View>
      )
    }

    return (
     <ScrollView style={styles.container}>
      <View style={styles.inputGroup}>
        <TextInput
          placeholder={'Task'}
          value={this.state.task}
          onChangeText={(val) => this.inputValueUpdate(val,'task')}
        />

        <Button
          onPress={() => this.storeTodo() }
          title="Tambah Task"
          color="#19AC52"
        />
      </View>


     </ScrollView>
    );
  }
}

Also, on my firebase console, I've change the rules read and write as TRUE already and that didn't affected.此外,在我的 firebase 控制台上,我已经将读写规则更改为TRUE并且没有受到影响。

and after the preload showed, there was an error message like this:在显示预加载后,出现如下错误消息:

Setting a timer for a long period of time, ie multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground.在Android上设置一个长时间的定时器,即多分钟,是一个性能和正确性问题,因为它保持定时器模块处于唤醒状态,并且定时器只能在应用程序处于前台时调用。 See https://github.com/facebook/react-native/issues/12981 for more info.有关更多信息,请参阅https://github.com/facebook/react-native/issues/12981

Something went wrong with my code and any idea to solve this error?我的代码出了点问题,有解决此错误的想法吗?

I had a similar issue couple of days ago, ended up using React Native Firebase library cause I was not able to connect to Firestore using official firebase npm module in react native.几天前我遇到了类似的问题,最终使用了React Native Firebase库,因为我无法使用官方 firebase npm 模块连接到 Firestore。

Its important to say that this library is recommended by google as seen on this page .重要的是要说这个库是谷歌推荐的,如本页所示。 And works pretty well.而且效果很好。

I stomped into this same warning recently, it's a bug between Firebase and React Native and it has been around since 2017我最近踩到了同样的警告,这是 Firebase 和 React Native 之间的错误,自 2017 年以来一直存在

https://github.com/firebase/firebase-js-sdk/issues/97 https://github.com/firebase/firebase-js-sdk/issues/97

Editing a parameter in a node-modules file fixes the warning ( https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-485410026 ) but it can have other problems associated and you have to make the same hotfix after each npm install.编辑节点模块文件中的参数可修复警告( https://github.com/firebase/firebase-js-sdk/issues/97#issuecomment-485410026 )但它可能有其他相关问题,您必须每个 npm 安装后的相同修补程序。

I haven't tried React Native Firebase package, maybe it's a cleaner way to get rid of the problem.我还没有尝试过 React Native Firebase package,也许这是摆脱问题的更清洁的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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