Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React: mutating stream data unnecessarily causes other functions being recreated even though they are enclosed by useCallback #2950

Open
tonyfarney opened this issue Sep 9, 2024 · 0 comments · May be fixed by #2951
Labels
ai/ui bug Something isn't working

Comments

@tonyfarney
Copy link

Description

In use-chat.ts:

...
onUpdate(merged, data) {
  mutate([...chatRequest.messages, ...merged], false);
  mutateStreamData([...(existingData || []), ...data], false);
},
...

data is an empty array in my use case and it is causing streamData being modified. As it is a dependency for triggerRequest and triggerRequest is a dependency for append, reload and handleSubmit, useCallback returns a new function every time an update occurs when reading from the stream.

A simple modification avoids this behavior (and consequently unnecessary re-renders):

... 
onUpdate(merged, data) {
  mutate([...chatRequest.messages, ...merged], false);
  if (data?.length) {
    mutateStreamData([...(existingData || []), ...data], false);
  }
},
...

Code example

No response

Additional context

No response

@lgrammel lgrammel added bug Something isn't working ai/ui labels Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/ui bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants