Simply put
const {
messages,
setMessages,
input,
setInput,
append
} = useChat({
onFinish: (message) => {
pushToLocalStateAndReset()
}
})
const pushToLocalStateAndReset = useCallback(() => {
console.log({
msg: 'pushToLocalStateAndReset',
messages: messages,
})
}, [messages])
On the first chat will output

Even though there is data within the messages[] array as it's render on the screen.
The problem
It's likely one of two things
- I'm using useCallback incorrectly because of my lack of experience with React.
- Something about the way that SWR implements
messages and it's mutate(...) function doesn't work like if messages was simply stored inside of useState for warning dependency that an update has occured.
See
|
const { data: messages, mutate } = useSWR<Message[]>([api, chatId], null, { |
// Store the chat state in SWR, using the chatId as the key to share states.
const { data: messages, mutate } = useSWR<Message[]>([api, chatId], null, {
fallbackData: initialMessages,
});
Simply put
On the first chat will output

Even though there is data within the messages[] array as it's render on the screen.
The problem
It's likely one of two things
messagesand it'smutate(...)function doesn't work like if messages was simply stored inside ofuseStatefor warning dependency that an update has occured.See
ai/packages/core/react/use-chat.ts
Line 352 in f81de39