Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 如何在react native中使用immutable.js和redux操作从firestore获取数据?_React Native_React Redux_Immutable.js_Redux Actions - Fatal编程技术网

React native 如何在react native中使用immutable.js和redux操作从firestore获取数据?

React native 如何在react native中使用immutable.js和redux操作从firestore获取数据?,react-native,react-redux,immutable.js,redux-actions,React Native,React Redux,Immutable.js,Redux Actions,我是英语初学者。如果你给我任何建议,我会很感激你的 我正在尝试使用react native,immutable.js,react redux,redux操作从firestore获取数据,而且这些代码遵循ducks结构。 问题是我无法从firestore中获取任何数据,因为firestore中的数据在reducer执行之后出现 甚至,我也不确定是否可以将firestorefetching方法作为createAction的第二个参数 这是我的密码 shared/index.js 从“firebas

我是英语初学者。如果你给我任何建议,我会很感激你的

我正在尝试使用
react native
immutable.js
react redux
redux操作
firestore
获取数据,而且这些代码遵循
ducks结构
。 问题是我无法从
firestore
中获取任何数据,因为
firestore
中的数据在reducer执行之后出现

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

这是我的密码


shared/index.js

从“firebase/app”导入firebase;
导入“firebase/firestore”
导入“firebase/storage”
常量配置={
apiKey:“…”,
authDomain:“…”,
数据库URL:“…”,
项目D:“…”,
storageBucket:“…”,
messagingSenderId:“…”
};
firebase.initializeApp(配置);
const db=firebase.firestore();
const storage=firebase.storage();
导出常量getCategoryNames=()=>{
返回db.collection('categories').get();
}

商店/模块/类别管理列表

从'redux actions'导入{createAction,handleActions};
从“不可变”导入{Map,List};
//火库
从“../../shared”导入*作为数据库;
//动作类型
const GET_NAME='categoryImgList/GET_NAME';
//动作创造者
export const getName=createAction(获取名称,()=>{
db.getCategoryNames().then(数据=>{
常数arr=[];
data.forEach(doc=>{
console.log(doc.data().name);
arr.push(doc.data().name);
});
console.log('arr');
控制台日志(arr);
})
}); // 没有一个
常量initialState=Map({
姓名:List()
});
//减速器
导出默认手动过滤({
[获取名称]:(状态,{payload:NAME})=>{
console.log(状态);
log({name});
const item=Map({name});
返回state.update('name',name=>name.push(项));
}
},初始状态);

非常感谢你

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

从'redux actions'导入{handleActions};
//火库
从“../../shared”导入*作为数据库;
//动作类型
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';
//动作创造者
导出常量getName=()=>async(分派)=>{
分派({type:GET_CATEGORY_NAME_PENDING});
试一试{
const response=await db.getCategoryNames();
常数arr=[];
response.docs.forEach(res=>{
arr.push(res.id);
});
控制台日志(arr);
分派({type:GET\u CATEGORY\u NAME\u SUCCESS,payload:arr});
返回arr;
}捕获(e){
控制台日志(e);
分派({type:GET\u CATEGORY\u NAME\u FAILURE,payload:e});
}
}
常量初始状态={
取景:假,
错误:false,
姓名:[]
};
//减速器
导出默认手动过滤({
[GET_CATEGORY_NAME_PENDING]:(state)=>({…state,fetching:true,error:false}),
[GET_CATEGORY_NAME_SUCCESS]:(状态,操作)=>({…状态,获取:false,名称:action.payload}),
[获取类别名称失败]:(状态)=>({…状态,获取:false,错误:true})
},初始状态);

我通常的做法是在从firestore检索数据后发送操作。不过,在本例中,除了记录数据之外,在将数据添加到数组中之后,看起来并没有对数据执行任何操作。您可以通过将其加载到数组中来处理分辨率,但我相信您必须返回数组或沿着这些线的其他内容。它应该如何进入有效载荷?谢谢你的回答!它应该以数组的形式获取数据,因此它应该是
[…name]