i am trying to use the new Gemini api, i am using server actions to get the user message from the input and sending that message to the gemini function which returns the response as text.
"use server";
import runChat from "@/utils/gemini";
export async function handleSubmit(formData) {
const prompt = formData.get("promptInput");
const res = await runChat(prompt);
return res;
}
Now i am trying to return the response from the handleSubmit function and get the text in an html div
import React from 'react'
const PromptBox = () => {
return (
<div className="promptBox h-[80vh] bg-[#1E1E1E] m-4 rounded-lg p-4 font-medium">
// I WANT THE RESPONSE TEXT TO COME HERE
</div>
);
}
export default PromptBox
This is what ive tried but its not working.
import { handleSubmit } from '@/actions/action';
import React from 'react'
const PromptBox = () => {
const response = handleSubmit
console.log(response)
return (
<div className="promptBox h-[80vh] bg-[#1E1E1E] m-4 rounded-lg p-4 font-medium">
{response}
</div>
);
}
export default PromptBox
im very sorry if there are any stupid mistakes im very new to react/nextJs
2
Answers
Use the state in the component. Set the response to the state and use that state in the html to display the response.
to build on Muhammad Taha’s response, your PromptBox should be managing the data as state. an example of what that could look like:
you are also not actually calling the "handleSubmit" function in your component. instead of "handleSubmit", try "handleSubmit()". and it also will require you to pass in a formData object, according to your own definition.
the react docs are pretty good about covering the basics, if this all didn’t make sense.
https://react.dev/reference/react/useState
good luck!