我是 React Native 的初学者。如果您给我任何建议,那将很有帮助,我将不胜感激。
我正在尝试firestore
使用react-native
, immutable.js
,获取数据react-redux
,redux-actions
此代码也如下所示ducks structure
。问题是我无法从中获取任何数据,firestore
因为数据firestore
来自于 reducer 执行之后。
甚至,我不确定是否可以将firestore
获取方法作为createAction
.
这是我的代码。
共享/index.js
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();
}
存储/模块/categoryImgList
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);
太感谢了!