Skip to content

Commit

Permalink
added memoization to stop rerenders
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrylimarti committed Oct 26, 2024
1 parent 9187d95 commit 3cecd0c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/react/src/hooks/useSetMessageList.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useMemo } from 'react';

export const useSetMessageList = (messages, shouldRender) => {
const [loading, setLoading] = useState(true);
const [messageList, setMessageList] = useState([]);

useEffect(() => {
setLoading(true);
const filteredMessages = messages.filter((message) =>
shouldRender(message)
);
const messageList = useMemo(
() => messages.filter(shouldRender),
[messages, shouldRender]
);

setMessageList(filteredMessages);
useEffect(() => {
setLoading(false);
}, [messages, shouldRender]);

Expand Down

0 comments on commit 3cecd0c

Please sign in to comment.