when someone clicks an item i want to store that clicked item in my likedjokes array. i mean empty state array. i tried this solution but instead of showing a new post it just shows post.id.
basically this is the result: image
and this is what i want image
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 [likedJokes, setLikedJokes] = useState([]);
useEffect(() => {
const getPosts = async () => {
const data = await getDocs(postsCollectionRef);
setPostList(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
};
getPosts();
}, []);
const addFavorite = (postLists) => {
setLikedJokes((prevlikedJokes) => [...prevlikedJokes, postLists]);
console.log(postLists);
};
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
linkin={post.linkin}
id={post.id}
title={post.title}
imageURL={post.imageURL}
photoURL={post.photoURL}
name={post.name}
addFavorite={addFavorite}
likedJokes={likedJokes}
setLikedJokes={setLikedJokes}
/>
</>
);
})}
</div>
</div>
{likedJokes}
</div>
</div>
);
};
export default Home;
PostList.js. im using firebase to get documents btw.
import React from "react";
const PostList = ({
linkin,
title,
post,
index,
imageURL,
photoURL,
name,
likeJoke,
likedJokes,
id,
setLikedJokes,
addFavorite,
}) => {
return (
<>
<div>
<div className="post" key={id}>
<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>
{likedJokes}
</div>
<div className="butons">
<button onClick={() => addFavorite(id)} id="favori">
+
</button>
</div>
</div>
</div>
</div>
</div>
</>
);
};
export default PostList;