I am developing a React Native app which has a chat as one of its main functions. I use a .useState([]) hook to store the chat's messages and FlatList to render the messages. When I am sending or receiving a message I do setMessages[...messages, {newMessageData}]. But after a certain amount of messages have been stored in the chat, it takes a while just to press send to a message (excluding server handling) until the message loads shows in the FlatList. How could I improve the performance so that it would function well enough for a good live chat?
const [messages, setMessages] = React.useState([{datetime: new Date(), content: "", sender: "", num: "amfu", new:[]}])
const addMessage = (sender, content, id, hasImage, image) => {
let data = {}
if(messages.length >= 0){
data = {"sender": sender, "content": content, "num": messages.length,
"read":true, "datetime": new Date(), "id": id, "reactions": {"user": "", "other": "", "has":false}, "hasImage": hasImage,
"image": hasImage ? image : null}
} else {
}
setMessages([data, ...messages])
setText("")
}
<FlatList
data={messages}
renderItem={handleM}
keyExtractor={(item) => item.num}
inverted={true}
style={{maxHeight: "90%", height: "auto", paddingTop: 100,}}
contentContainerStyle={{ flexGrow: 1 }}
ref={listRef}
scrollEnabled={isLongPressed == ""}
/>