ai icon indicating copy to clipboard operation
ai copied to clipboard

useChat key parameter nonfunctional

Open ekatzenstein opened this issue 1 year ago • 3 comments

Description

I'm using a key to refresh useChat and it's non-reactive with new initialMessages:

Code example

  const { messages, input, handleInputChange, handleSubmit } = useChat({
    initialMessages: gptMessages as Message[],
    onFinish,
    key: chatMode,
  });

Additional context

chatMode is changing dynamically to refresh the session but the initialMessages don't update.

ekatzenstein avatar Jan 03 '24 21:01 ekatzenstein

Can you use setMessages(gptMesssages) in an effect?

MaxLeiter avatar Jan 04 '24 19:01 MaxLeiter

It's not completed. There is specifically a "key" property that doesn't work. One can use setMessages in useEffect, but that's completely unrelated to this issue. It is not a workaround.

ekatzenstein avatar May 12 '24 10:05 ekatzenstein

Workaround would be isolating your useChat hook into a component like ChatMessages, and then having the key be used on that component.

For example,

<ChatMessages key={chat.id}/>

BrianHung avatar Aug 16 '24 18:08 BrianHung