ai
ai copied to clipboard
useChat key parameter nonfunctional
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.
Can you use setMessages(gptMesssages) in an effect?
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.
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}/>