reactjs - 错误:除非您的组件在 FirebaseAppProvider 中,否则无法调用 useFirebaseApp
问题描述
当我进入 createNewPost 页面时出现此错误,我不知道为什么,您能帮帮我吗?问题是什么?其他页面(主页和个人资料页面)没有任何问题。
(我研究过,但我找不到任何东西。)
错误:除非您的组件在 FirebaseAppProvider 中,否则无法调用 useFirebaseApp
我的 createNewPost 页面代码在这里:
const CreateNewPost = () => {
const { user } = useAuth();
const uid = firebase.auth().currentUser.uid;
const postRef = useFirestore()
.collection("users")
.doc(uid)
.collection("posts");
const [post, setPost] = useState({
content: "",
imageURL: "",
});
const createPost = async () => {
await postRef.add({
content: post.content,
imageURL: post.imageURL,
timezone: dateFormat(),
});
setPost({ content: "", imageURL: "" });
};
return (
<>
<div className="container">
<form>
<div class="form-group">
<div className="d-flex justify-content-start ">
<div className="d-flex align-items-center">
<img
className="rounded-circle py-2"
alt="profile"
src={user.photoURL}
style={{
maxHeight: "80px",
maxWidth: "80px",
marginBottom: "-17px",
}}
/>
<h2 style={{ marginLeft: "7px", marginTop: "16px" }}>
{user.displayName}
</h2>
</div>
</div>
<hr></hr>
<textarea
class="form-control"
id="exampleFormControlTextarea1"
rows="23"
placeholder="Enter a description..."
onChange={(e) => setPost({ content: e.target.value })}
></textarea>
</div>
</form>
<input
class="form-control"
type="text"
placeholder="Enter the image link"
style={{ marginTop: "7px" }}
onChange={(e) => setPost({ imageURL: e.target.value })}
></input>
<div className="d-flex justify-content-end">
<button
type="button"
class="btn btn-primary btn-lg"
style={{ marginTop: "3px" }}
onClick={createPost}
>
Post
</button>
</div>
</div>
</>
);
};
解决方案
我像这样在 index.jsx 中使用了 Firebase 应用程序提供程序,我解决了:
ReactDOM.render(
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
<App />
</FirebaseAppProvider>
,
document.getElementById('root')
);
推荐阅读
- c++ - 带有 clang++ 的 throw() 显示可能的内存泄漏
- c# - Net Core Identity - 在外部注册时询问用户缺少的信息/要求接受 Ts & Cs
- jquery - ASP.net MVC 控制器从 Ajax 发送时接收空数组
- angular - *ngFor 循环中的多个输入文件的问题
- java - jar 顶层内的多个入口点(主类)
- javascript - Firebase 实时数据库规则防止节点删除
- sql-server - 如何为在 Docker for MSSQL Server 中运行的 Kafka 设置 Debezium
- mysql - 单个 SQL 连接可以运行并发事务吗?
- php - 使用 XMLreader 和 PHP 进行搜索和获取
- php - 使用 JSON 原始数据 Laravel 搜索 SQL