Skip to content

Commit b07439f

Browse files
committed
perf: default warp BubbleList with React.memo
1 parent bbc0546 commit b07439f

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

.changes/memo-bubble-list.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@matechat/react": patch:perf
3+
---
4+
5+
Default wrap `BubbleList` with `React.memo` to avoid extra rerender.

playground/src/Chat.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MessageSquarePlus } from "lucide-react";
2-
import { useCallback, useState } from "react";
2+
import { useMemo, useState } from "react";
33
import { BubbleList } from "../../dist/bubble";
44
import { Button } from "../../dist/button";
55
import { FileUpload } from "../../dist/file-upload";
@@ -50,9 +50,17 @@ export function Chat() {
5050
initialMessages,
5151
);
5252

53-
const onClear = useCallback(() => {
54-
setPrompt("");
55-
setMessages([]);
53+
const footer = useMemo(() => {
54+
const onClear = () => {
55+
setPrompt("");
56+
setMessages([]);
57+
};
58+
return (
59+
<Button onClick={onClear} variant="default" className="self-center">
60+
<MessageSquarePlus size="1.1rem" />
61+
Start a new conversation
62+
</Button>
63+
);
5664
}, [setMessages]);
5765

5866
return (
@@ -63,12 +71,7 @@ export function Chat() {
6371
messages={messages}
6472
background="right-solid"
6573
isPending={isPending}
66-
footer={
67-
<Button onClick={onClear} variant="default" className="self-center">
68-
<MessageSquarePlus size="1.1rem" />
69-
Start a new conversation
70-
</Button>
71-
}
74+
footer={footer}
7275
/>
7376
{messages.length === 0 && (
7477
<Prompts>

src/bubble.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import "./tailwind.css";
33

44
import clsx from "clsx";
55
import type React from "react";
6-
import { useCallback, useEffect, useRef, useState } from "react";
6+
import { memo, useCallback, useEffect, useRef, useState } from "react";
77
import Markdown from "react-markdown";
88
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
99
import {
@@ -247,7 +247,7 @@ export interface BubbleListProps extends React.ComponentProps<"div"> {
247247
threshold?: number;
248248
}
249249

250-
export function BubbleList({
250+
export const BubbleList = memo(function BubbleList({
251251
className,
252252
background = "right-solid",
253253
footer,
@@ -424,4 +424,4 @@ export function BubbleList({
424424
)}
425425
</div>
426426
);
427-
}
427+
});

0 commit comments

Comments
 (0)