我怎样才能通过这个组件
我想在我的收藏夹组件中显示“收藏夹”项目。但由于我无法将 postList 组件传递给收藏夹组件,我不知道该怎么做。
基本上我想在另一个页面/组件而不是 home.js 中显示这个最喜欢的项目。
Home.js
import React, { useState, useEffect } from "react";
import { getDocs, collection, deleteDoc, doc } from "firebase/firestore";
import { db, auth } from "../../firebase";
import { Link } from "react-router-dom";
import Sidebar from "../Sidebar/Sidebar";
import "./Home.css";
import PostList from "./PostList";
const Home = ({ isAuth, setIsAuth }) => {
const [postLists, setPostList] = useState([]);
const postsCollectionRef = collection(db, "posts");
const [favorites, setFavorites] = useState(localStorage.getItem("dam"));
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
};
getPosts();
}, []);
useEffect(() => {
const localData = localStorage.getItem("dam") ?? [];
setFavorites(localData);
}, [setFavorites]);
const addFavorite = (favorite) => {
setFavorites((prevfavorites) => [...prevfavorites, favorite]);
localStorage.setItem("dam", JSON.stringify(favorites));
};
return (
<div className="containers">
<div className="sidebar">
<Sidebar isAuth={isAuth} setIsAuth={setIsAuth} />
<div className="centered">
<div className="bordered">
<button id="ado">
<Link to="/createpost">+ Add API</Link>
</button>
</div>
<div className="new-container">
{postLists?.map((post) => {
return (
<>
<PostList
post={post}
addFavorite={addFavorite}
key={post.id}
/>
</>
);
})}
</div>
</div>
<div className="another">
<h2>FAVORITE ITEMS</h2>
{postLists
.filter((post) => favorites.includes(post.id))
.map((post) => (
<PostList post={post} addFavorite={addFavorite} key={post.id} />
))}
</div>
</div>
</div>
);
};
export default Home;
PostList.js
import React from "react";
const PostList = ({ post, addFavorite }) => {
const { linkin, title, imageURL, photoURL, name, id } = post;
return (
<>
<div>
<div className="post">
<div className="postimage">
<div className="del"></div>
<div className="images">
<a href={linkin}>
<p className="ss">{title}</p>
<img src={imageURL} id="img-photo" />
</a>
<div className="uploader">
<img src={photoURL} />
<p>by {name}</p>
</div>
{addFavorite && (
<div className="butons">
<button onClick={() => addFavorite(id)} id="favori">
+
</button>
</div>
)}
</div>
</div>
</div>
</div>
</>
);
};
export default PostList;
和一个空的Favorites.js 组件。
收藏夹.js
favorites here...
i want to show "favorite" items in my favorites component. but since i can't pass postList component to favorites component i don't know how to do it.
basically i want to show this favorite items in another page/component instead of home.js.
Home.js
import React, { useState, useEffect } from "react";
import { getDocs, collection, deleteDoc, doc } from "firebase/firestore";
import { db, auth } from "../../firebase";
import { Link } from "react-router-dom";
import Sidebar from "../Sidebar/Sidebar";
import "./Home.css";
import PostList from "./PostList";
const Home = ({ isAuth, setIsAuth }) => {
const [postLists, setPostList] = useState([]);
const postsCollectionRef = collection(db, "posts");
const [favorites, setFavorites] = useState(localStorage.getItem("dam"));
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
};
getPosts();
}, []);
useEffect(() => {
const localData = localStorage.getItem("dam") ?? [];
setFavorites(localData);
}, [setFavorites]);
const addFavorite = (favorite) => {
setFavorites((prevfavorites) => [...prevfavorites, favorite]);
localStorage.setItem("dam", JSON.stringify(favorites));
};
return (
<div className="containers">
<div className="sidebar">
<Sidebar isAuth={isAuth} setIsAuth={setIsAuth} />
<div className="centered">
<div className="bordered">
<button id="ado">
<Link to="/createpost">+ Add API</Link>
</button>
</div>
<div className="new-container">
{postLists?.map((post) => {
return (
<>
<PostList
post={post}
addFavorite={addFavorite}
key={post.id}
/>
</>
);
})}
</div>
</div>
<div className="another">
<h2>FAVORITE ITEMS</h2>
{postLists
.filter((post) => favorites.includes(post.id))
.map((post) => (
<PostList post={post} addFavorite={addFavorite} key={post.id} />
))}
</div>
</div>
</div>
);
};
export default Home;
PostList.js
import React from "react";
const PostList = ({ post, addFavorite }) => {
const { linkin, title, imageURL, photoURL, name, id } = post;
return (
<>
<div>
<div className="post">
<div className="postimage">
<div className="del"></div>
<div className="images">
<a href={linkin}>
<p className="ss">{title}</p>
<img src={imageURL} id="img-photo" />
</a>
<div className="uploader">
<img src={photoURL} />
<p>by {name}</p>
</div>
{addFavorite && (
<div className="butons">
<button onClick={() => addFavorite(id)} id="favori">
+
</button>
</div>
)}
</div>
</div>
</div>
</div>
</>
);
};
export default PostList;
and a empty Favorites.js component.
Favorites.js
favorites here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 React 自定义钩子或 useContext 钩子。
通过这两个选项,您可以共享和访问收藏夹组件中的收藏夹项目。这些钩子有助于全局共享状态。
Why don't you use a react custom hook, or useContext hook.
With this two option, you can share and access the favorite items in your favorites component. These hooks help share state globally.