Оборачивание вызова localStorage в Promise усложняет задачу. Вам нужно сделать вызов firestore, когда электронная почта изменится. Ниже приведены два решения, которые я могу предложить.
- Первое запускается только тогда, когда компонент монтирует и удаляет электронную почту из состояния.
const [people, setPeople] = useState([]);
useEffect(() => {
// you might need to wrap this in a try catch as it may fail if a user has disabled access to localStorag
const email = localStorage.getItem("userid");
if (email) {
fire
.firestore()
.collection("Users")
.where("Email", "==", email)
.get()
.then((snapshot) => {
console.log(snapshot);
setPeople(snapshot.docs[0].data().Name);
})
.catch((e) => {
console.log(e);
});
}
}, []);
Если вы хотите, чтобы эффект огненного хранилища запускался при изменении электронного письма. Вы можете разделить эти два эффекта и сделать так, чтобы эффект хранилища зависел от изменений электронной почты, как показано ниже.
const [people, setPeople] = useState([]);
const [email, setEmail] = useState('');
useEffect(() => {
const localStorageEmail = localStorage.getItem("userid");
if (email) {
setEmail(localStorageEmail);
}
}); // dependence array ignored will run everytime the component rerenders
useEffect(() => {
if (!email) return;
fire
.firestore()
.collection("Users")
.where("Email", "==", email)
.get()
.then((snapshot) => {
console.log(snapshot);
setPeople(snapshot.docs[0].data().Name);
})
.catch((e) => {
console.log(e);
});
}, [email]);