如何写入来自Firebase的数据以快速存储? [英] How can i write data that is coming from Firebase to store quickly?

查看:64
本文介绍了如何写入来自Firebase的数据以快速存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我正在使用React Native.我从Firebase获取数据,并想快速写入存储区(由Redux提供).但这是行不通的.您可以在下面找到我的所有代码:

Firstly, I'm working with React Native. I'm getting a data from Firebase and want to write to store (by Redux) quickly. But It doesn't work. You can find my all of codes below:

功能

async getTumData (uid) {

    const {selectedGroupDetail, getSelectedGroupDetail} = this.props;
    var yeniGrupDetayi = {};
    await firebase.database().ref("/groups/"+uid).once('value').then(
      function(snapshot){
        yeniGrupDetayi = {...snapshot.val(), uid: uid};
      }).catch(e => console.log(e.message));

      console.log("FONKSIYON ICERISINDEKI ITEM ==>", yeniGrupDetayi);
      this.props.getSelectedGroupDetail(yeniGrupDetayi);
      console.log("ACTION'DAN GELEN ITEM ===>", selectedGroupDetail);

  }

操作:

export const getSelectedGroupDetail = (yeniGrupDetayi) => {
  return {
    type: GET_SELECTED_GROUP_DETAIL,
    payload: yeniGrupDetayi
  }
};

减速器:

case GET_SELECTED_GROUP_DETAIL:
      return { ...state, selectedGroupDetail: action.payload}

Çıktı:

FONKSIYON ICERISINDEKI ITEM ==> {admin: {…}, groupDescription: "Yaygın inancın tersine, Lorem Ipsum rastgele sözcü…erini incelediğinde kesin bir kaynağa ulaşmıştır.", groupName: "İnsan Kaynakları", groupProfilePic: "", members: {…}, …}

ACTION'DAN GELEN ITEM ===> {}

我的页面中有一个FlatList,我在FlatList的renderItem中定义了一个按钮.当我单击此按钮时,getTumData()功能正在工作.

There is a FlatList in my page and I defined a button in renderItem of FlatList. When i click to this button, getTumData() function is working.

当我第一次单击此按钮时,selectedGroupDetailnull.第二次显示以前的数据.

When i click to this button first time, selectedGroupDetail is null. Second time, it shows previous data.

如何快速,快速地将数据写入存储?

How can i write a data to Store quickly and fast?

谢谢

推荐答案

问题是: -您同时使用了异步/等待,然后/捕获了您的代码. -您要在异步代码解析之前调用getSelectedGroupDetail.

The thing is: - You're using both async/await, and then/catch in your code. - you're calling getSelectedGroupDetail before your async code resolves.

快速解决方案

getTumData =  (uid) => {

    const {selectedGroupDetail, getSelectedGroupDetail} = this.props;
    var yeniGrupDetayi = {};
    firebase.database().ref("/groups/"+uid).once('value').then(
     (snapshot) => {
        yeniGrupDetayi = {...snapshot.val(), uid: uid};
        this.props.getSelectedGroupDetail(yeniGrupDetayi);
      }).catch(e => console.log(e.message));   
  };

更好的解决方案:

第一:使用Redux-Thunk中间件. 第二:将您的异步代码移到动作创建者中:我的意思是

1st: use Redux-Thunk middleware. 2nd: Move your Async code into your action creator: I mean this

async getTumData (uid) {

    const {selectedGroupDetail, getSelectedGroupDetail} = this.props;
    var yeniGrupDetayi = {};
    await firebase.database().ref("/groups/"+uid).once('value').then(
      function(snapshot){
        yeniGrupDetayi = {...snapshot.val(), uid: uid};
      }).catch(e => console.log(e.message));

      console.log("FONKSIYON ICERISINDEKI ITEM ==>", yeniGrupDetayi);
      this.props.getSelectedGroupDetail(yeniGrupDetayi);
      console.log("ACTION'DAN GELEN ITEM ===>", selectedGroupDetail);

  }

3rd:在您的selectedGroupDetail解析之前,reducer应该具有另一条数据作为时间间隔的指示器:

3rd: Your reducer should have another piece of data as an indicator for the time-gap before your selectedGroupDetail resolves:

// reducer initial state:
const INITIAL_STATE = { error: '', loading: false, selectedGroupDetail: null }

4th:在动作创建者内部,您应该分派3个动作: ACTION_NAME_START//仅应在化简器中将load设置为true. ACTION_NAME_SUCCESS//将loading设置为false,并将selectedGroupDetail设置为新的集合 ACTION_NAME_FAIL//如果操作失败,则设置错误

4th: Inside your action creator, you should dispatch 3 actions: ACTION_NAME_START // This should should only set loading to true in your reducer. ACTION_NAME_SUCCESS // set loading to false, and selectedGroupDetail to the new collection retured ACTION_NAME_FAIL // in case op failed set error

第5条:您的React组件应显示一个加载指示器(旋转器或东西),并可能在加载状态下禁用FlatList按钮.

5th: Your React component, should display a loading indicator (spinner or somthing), and maybe disable FlatList button during the loading state.

// Action creator
export const myAction = () => (dispatch) => {
  dispatch({ type: ACTION_NAME_START });
  firebase.database().ref("/groups/"+uid).once('value').then(
  function(snapshot){
    yeniGrupDetayi = {...snapshot.val(), uid: uid};
    dispatch({ type: ACTION_NAME_SUCCESS, payload: yeniGrupDetayi  });

  }).catch(e => {
  dispatch({ type: ACTION_NAME_FAIL, payload: e.message });
});

};


// Reducer
const INITIAL_STATE = {
  loading: false,
  error: '',
  data: null,
};

export default (state = INITIAL_STATE, { type, payload }) => {
  switch (type) {
    case ACTION_NAME_START:
      return {
        ...state,
        error: '',
        loading: true,
        data: null,
      };

    case ACTION_NAME_SUCCESS:
      return {
        ...state,
        error: '',
        loading: false,
        data: payload,
      };

    case ACTION_NAME_FAIL:
      return {
        ...state,
        error: payload,
        loading: false,
        data: null,
      };

    default:
      return state;
  }
};

这篇关于如何写入来自Firebase的数据以快速存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆