大佬教程收集整理的这篇文章主要介绍了ReactJS 多个 useState 挂钩未更新 Promise 中的状态解析,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个函数可以触发 onClick 并且应该将一组状态保存到 Firebase。由于异步,有两种状态可能会失败,因此我尝试将它们全部链接到 Promises 中。 Promises 肯定是正确解析的,但是当我尝试使用 useState 钩子时,它没有更新解析中的状态。所以我尝试以下面的方式链接它,同样的错误(函数 CollectionReference.doc() 不能用空路径调用。)仍然返回。为什么我的 mID 和 LASTUPDATED 状态为空?
const db = firebaseApp.firestore();
const initialFIEldValues = {
mID: "",LASTUPDATED: "",};
const [store,setStore] = useState(initialFIEldValues);
const createMID = () => {
return new Promise((resolve,reject) => {
let docID = db.collection("merchant").doc().ID;
let key = "mID-".concat(docID);
if (key) {
resolve(key);
} else {
reject("Failed to create mID key");
}
});
};
const gettimestamp = () => {
return new Promise((resolve,reject) => {
let timestamp = firebase.firestore.FIEldValue.servertimestamp();
if (timestamp) {
resolve(timestamp);
} else {
reject("Failed to get timestamp");
}
});
};
const registerMerchant = () => {
createMID()
.then(
(data) => {
setStore({
...store,mID: data,});
console.log(data); //correctly returns data
console.log(store.mID); //returns empty; setStore isn't setTing the state
return gettimestamp();
}).then(
(data2) => {
setStore({
...store,LASTUPDATED: data2,})
console.log(data2); //returns object
console.log(store.mID); //returns empty
}
).then(
() => {
if(!merchant && authenticated){
dispatch({
type: "SET_MERCHANT",merchant: store,});
} else if(merchant) {
return
}else{
dispatch({
type: "SET_MERCHANT",merchant: null,});
}
}
).then( ()=> {
// e.preventDefault();
db.collection('merchant').doc(store.mID).set(storE);
}).catch(error => alert(error.messagE))
}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的ReactJS 多个 useState 挂钩未更新 Promise 中的状态解析全部内容,希望文章能够帮你解决ReactJS 多个 useState 挂钩未更新 Promise 中的状态解析所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。