在react native中,在触发firebase上的更新之前,如何等待redux命令更改存储以完成执行
我基本上是在开发一个人们可以提问的应用程序。因此,当用户发布新问题时,我会更新我的redux存储。然后我将更改推送到firebase。但问题是,新的更改并没有反映在firebase中,因为redux更新需要一些时间,到那时firebase更新函数已经被调用。我如何改变这个 编码我在哪里更新我的商店并调用firebase更新在react native中,在触发firebase上的更新之前,如何等待redux命令更改存储以完成执行,firebase,react-native,redux,google-cloud-firestore,Firebase,React Native,Redux,Google Cloud Firestore,我基本上是在开发一个人们可以提问的应用程序。因此,当用户发布新问题时,我会更新我的redux存储。然后我将更改推送到firebase。但问题是,新的更改并没有反映在firebase中,因为redux更新需要一些时间,到那时firebase更新函数已经被调用。我如何改变这个 编码我在哪里更新我的商店并调用firebase更新 this.props.newQuestion(this.state.addedData); console.log("Hello there "
this.props.newQuestion(this.state.addedData);
console.log("Hello there " + JSON.stringify(this.props.user));
let temp = this.props.user;
console.log("This is temp" + JSON.stringify(temp));
firestore()
.collection("users")
.doc(this.props.user.id)
.update({
user: temp,
})
.then(() => {
console.log("From the then" + JSON.stringify(this.props.user));
console.log("User updated!");
})
.catch((err) => {
console.log(err);
});
此redux更新函数的我的调度程序定义-
case ADD_QUESTION:
//console.log("From the actions" + JSON.stringify(action.question));
return {
...state,
user: {
id: state.user.id,
name: state.user.name,
email: state.user.email,
courses: state.user.courses,
// adding the new question everytime
questions: state.user.questions.concat(action.question),
},
};
Tushar,我遵循Kokovin Vladislav的想法和回答,请检查。请看下面的链接和他的答案
希望对您有所帮助,这个答案解释得很好。这个答案确实对我有帮助。此外,由于某些原因,当我尝试在android仿真器上运行我的应用程序时,它成功地构建了,但随后它没有在仿真器上运行。上面说应用程序停止了。我正在我的应用程序中使用firebase。你认为这个错误与什么有关?我能调试它吗?你刚刚出错了?你实现了什么,或者上面的任何库或任何代码?不,我没有因为添加此代码而收到错误,但我有一段时间没有收到此错误,但我继续为ios开发,认为稍后会对其进行排序。你为其安装了pod吗
component should be updated to receive new props.
there are ways to achieve your goal:
1. componentDidUpdate check if value is changed, then do something..
componentDidUpdate(prevProps){
if(prevProps.value !== this.props.value){ alert(prevProps.value) }
}
2. redux-promise ( middleware will dispatch the resolved value of the promise)
export const updateState = (key, value)=>
Promise.resolve({
type:'UPDATE_STATE',
key, value
})
then in component
this.props.dispatch(updateState(key, value)).then(()=>{
alert(this.props.value)
})
2. redux-thunk
export const updateState = (key, value) => dispatch => {
dispatch({
type: 'UPDATE_STATE',
key,
value,
});
return Promise.resolve();
};
then in component
this.props.dispatch(updateState(key, value)).then(()=>{
alert(this.props.value)
})