小编Sau*_*man的帖子

forEach 循环返回值,但我的 React useState 值仅返回一个数组 obj

const user = useSelector((state) => state.user);
const [allPost, setAllPost] = useState([]);

const getAllPost = async () => {
  const q = query(collection(db, "posts", "post", user.user.email));
  const querySnapshot = await getDocs(q);
  querySnapshot.forEach((doc) => {
    console.log(doc.data());
    const newPost = doc.data();
    setAllPost([...allPost, newPost]);
  });
};

useEffect(() => {
  getAllPost();
}, []);
Run Code Online (Sandbox Code Playgroud)

我的 firestore 中有四个文档,但 setState 仅返回一个文档,尽管使用 forEach 循环我可以看到所有四个对象

javascript arrays reactjs google-cloud-firestore use-state

1
推荐指数
1
解决办法
717
查看次数