【发布时间】:2018-09-18 21:03:51
【问题描述】:
我正在尝试找出将 firestore.onSnapshot 与 react-redux 一起使用的正确方法。
我目前在我的操作文件中有此代码,我在我的组件中调用 componentWillMount()。
export const fetchCheckins = () => async (dispatch) => {
const {currentUser} = firebaseService.auth();
try {
let timestamp = (new Date());
//set timestamp for beginning of today
timestamp.setHours(0);
//get checkins today
let checkinstoday = (await firebaseService.firestore().collection(`/checkins/${currentUser.uid}/log`).where("timestamp",">=",timestamp).orderBy("timestamp","desc").get()).docs.map(doc => doc.data());
//set timestamp for beggining of week
timestamp.setDate(-(timestamp.getDay()));
//get checkins (week)
let checkinsweek = (await firebaseService.firestore().collection(`/checkins/${currentUser.uid}/log`).where("timestamp",">=",timestamp).orderBy("timestamp","desc").get()).docs.map(doc => doc.data());
//set timestamp for begging of month
timestamp.setDate(0);
//get checkins (month)
let checkinsmonth = (await firebaseService.firestore().collection(`/checkins/${currentUser.uid}/log`).where("timestamp",">=",timestamp).orderBy("timestamp","desc").get()).docs.map(doc => doc.data());
dispatch({type: FETCH_CHECKINS, payload: { today: checkinstoday, week: checkinsweek, month: checkinsmonth}});
}
catch(e){
console.error(e);
}
};
这工作正常,正确的数据被发送到组件并显示。问题是,如果用户签入,签入数据应该调整,但它不会,因为我只获取一次数据并发送它,并且状态不会重新渲染。
我的问题是我应该如何处理这个问题?我是否使用.onSnapshot() 而不是.get()?我是否从.checkin() 动作创建者那里调用.fetchCheckins()?我如何根据最佳实践进行处理?谢谢
【问题讨论】:
标签: firebase react-native react-redux google-cloud-firestore redux-thunk