nlux icon indicating copy to clipboard operation
nlux copied to clipboard

Using a custom avatar make multiple render

Open ScreamZ opened this issue 1 year ago β€’ 4 comments

Hello,

Using a custom avatar like so:

 personaOptions={{
              user: {
                name: user.displayName ?? "πŸ‘Ύ",
                avatar: (
                  <AppAvatar
                    fallbackTextSize={12}
                    avatar={user.image ?? undefined}
                    name={user.displayName ?? "Anonymous"}
                  />
                ),
              },

            }}

makes AppAvatar to trigger re-render on each keystroke while writing a message, which in some case might trigger network request in chain

https://github.com/nlkitai/nlux/assets/6640835/a9c024f4-f623-4878-a6ea-e4d1a94dcec1

Might be an issue with react key ? Nothing change if using memo too.

ScreamZ avatar Jul 05 '24 13:07 ScreamZ

Hi @ScreamZ

In your example, what you need to do is to memoize the avatar.

const myAvatar = useMemo(() => (
    <AppAvatar
        fallbackTextSize={12}
        avatar={user.image ?? undefined}
        name={user.displayName ?? "Anonymous"}
    />
  ), [ /* dependencies */ ]);

Then

 personaOptions={{
              user: {
                name: user.displayName ?? "πŸ‘Ύ",
                avatar: myAvatar,
              },
            }}

I hope all those unnecessary memoizations will become obsolete with React 19, but until then, they're still needed. Give it a try and let me know how it works for you ..

salmenus avatar Jul 08 '24 13:07 salmenus

Same issue, ;(

ScreamZ avatar Jul 08 '24 13:07 ScreamZ

Hi @salmenus I am also running into this and did some react profiling to understand the extent of the problem

It looks like the entire AI Chat React component is re-rendering on every keystroke when writing a message. This issue is affecting both the Avatars and Response Renderers. I was able to confirm by using the playground example to make sure it was not any code that I introduced

I'm just using the basic react example and ran a profiling. On every key press this hook is causing issues and the reason react.memo does not work is because by the time it gets to Avatar and Renderer. It says "This is the first time component rendered" So memo does not work

CleanShot 2024-07-28 at 10 37 08@2x

CleanShot 2024-07-28 at 10 36 44@2x

lohit8846 avatar Jul 28 '24 17:07 lohit8846

Investigating this as part of #122

salmenus avatar Jul 31 '24 14:07 salmenus