【问题标题】:How can I add loader state to firebase's createUserWithEmailAndPassword method?如何将加载程序状态添加到 firebase 的 createUserWithEmailAndPassword 方法?
【发布时间】:2022-10-17 22:35:11
【问题描述】:
当我使用电子邮件和密码拨打createUserWithEmailAndPassword 时,我想为我的按钮设置一个加载程序。我怎样才能做到这一点?
例如,在 Apollo Client GraphQL 中,提供了一个开箱即用的加载状态,例如:
const {data, loading, error} = <API call>
如果数据获取正在进行中,给定的加载状态会自动设置为 true。
有没有类似的方式我们可以在firebase中做到这一点?
【问题讨论】:
标签:
reactjs
firebase-authentication
【解决方案1】:
没有与反应状态相关的内置函数,因此您可以自己创建状态:
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
// Whatever function is going to do the loading:
const onClick = async () => {
try {
setLoading(true);
const userCredential = await createUserWithEmailAndPassword(email, password);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
}