首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用immutable.js和redux操作从防火墙中获取数据?

如何使用immutable.js和redux操作从防火墙中获取数据?
EN

Stack Overflow用户
提问于 2018-06-15 23:59:38
回答 1查看 687关注 0票数 2

我是个反应本土化的初学者。如果你给我任何建议,这将是有帮助的,我会感谢你。

我正在尝试使用firestorereact-nativeimmutable.jsreact-reduxredux-actions获取数据,这些代码也遵循ducks structure。问题是我无法从firestore获得任何数据,因为来自firestore的数据是在还原器执行后产生的。

甚至,我也不确定是否可以将firestore获取方法作为createAction的第二个参数。

这是我的密码。

shared/index.js

代码语言:javascript
复制
import firebase from 'firebase/app';
import 'firebase/firestore'
import 'firebase/storage'

const config = {
  apiKey: "...",
  authDomain: "...",
  databaseURL: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "..."
};

firebase.initializeApp(config);
const db = firebase.firestore();
const storage = firebase.storage();

export const getCategoryNames = () => {
  return db.collection('categories').get();
}

store/modules/categoryImgList

代码语言:javascript
复制
import {createAction, handleActions} from 'redux-actions';
import { Map, List } from 'immutable';

// firestore
import * as db from '../../shared';

// Action types
const GET_NAME = 'categoryImgList/GET_NAME';

// action creator
export const getName = createAction(GET_NAME, () => {
  db.getCategoryNames().then(data => {
    const arr = [];

    data.forEach(doc => {
        console.log(doc.data().name);
        arr.push(doc.data().name);
    });

    console.log('arr');
    console.log(arr);
  })
}); // none

const initialState = Map({
    name: List()
});

// Reducer
export default handleActions({
    [GET_NAME]: (state, { payload: name }) => {
      console.log(state);
      console.log({name});
      const item = Map({ name });
      return state.update('name', name => name.push(item));
    }
}, initialState);

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-16 10:44:49

我自己解决了这个问题。我意识到我需要redux-thunk来获取异步数据。我安装了redux-thunk并修改了如下代码。

代码语言:javascript
复制
import {handleActions} from 'redux-actions';

// firestore
import * as db from '../../shared';

// Action types
const GET_CATEGORY_NAME_PENDING = 'categoryImgList/GET_CATEGORY_NAME_PENDING';
const GET_CATEGORY_NAME_SUCCESS = 'categoryImgList/GET_CATEGORY_NAME_SUCCESS';
const GET_CATEGORY_NAME_FAILURE = 'categoryImgList/GET_CATEGORY_NAME_FAILURE';

// action creator
export const getName = () => async (dispatch) => {
  dispatch({type: GET_CATEGORY_NAME_PENDING});
  try {
    const response = await db.getCategoryNames();

    const arr = [];
    response.docs.forEach(res => {
        arr.push(res.id);
    });
    console.log(arr);

    dispatch({type: GET_CATEGORY_NAME_SUCCESS, payload: arr});
    return arr;
  } catch (e) {
      console.log(e);
      dispatch({type: GET_CATEGORY_NAME_FAILURE, payload: e});
  }
}

const initialState = {
  fetching: false,
  error: false,
  name: []
};

// Reducer
export default handleActions({
  [GET_CATEGORY_NAME_PENDING]: (state) => ({ ...state, fetching: true, error: false }),
  [GET_CATEGORY_NAME_SUCCESS]: (state, action) => ({ ...state, fetching: false, name: action.payload }),
  [GET_CATEGORY_NAME_FAILURE]: (state) => ({ ...state, fetching: false, error: true })
}, initialState);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50883954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档