I'm using openAi API to talk to ChatGPT in my React App,
I want to send the request to ChatGPT after my message is asynchronously added to messages
array,
Is there a way to do it ?
function addMessage() {
setMessages([...messages, { writer: "Me", text: "Hi ChatGPT, how are you ?" }]);
// execute this after the asynchronous setState above is over
getResponse();
}
function getResponse() {
getOpenAiCompletions(text, temp).then((response) => {
setMessages([
...messages,
{ writer: "ChatGPT", text: response.data.choices[0].text },
]);
});
}