I have a problem, after the version 9 update of Firebase, I cant seem to figure out how to order the documents in a collection.
Here is my code. I do not seem to understand how to implement the orderBy in the onSnapshot. Can someone please nudge me in the right direction. I have looked through the documentation but could not find a solution.
import React, { useState, useEffect } from "react";
import SignOut from "./SignOut";
import { Grid, Typography, Avatar } from "@material-ui/core";
import db from "../firebase";
import { onSnapshot, collection, orderBy, query, limit } from "firebase/firestore";
import SendMessage from "./SendMessage";
function ChatBox() {
const [messages, setMessages] = useState([]);
useEffect(() => {
onSnapshot(collection(db, "messages"), (snapshot) => {
setMessages(snapshot.docs.map((doc) => doc.data()));
});
}, []);
return (
<>
<Grid container>
<Grid item xs="12" style={{ textAlign: "right" }}>
<SignOut></SignOut>
</Grid>
{messages.map(({ id, text, photoURL }) => (
<Grid item xs="12" key={id} style={{ marginBottom: "1rem" }}>
<Avatar alt="" src={photoURL} />
<Typography>{text}</Typography>
</Grid>
))}
</Grid>
<Grid container>
<SendMessage></SendMessage>
</Grid>
</>
);
}
export default ChatBox;