From the course: AI for React Developers
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Building a server action - React.js Tutorial
From the course: AI for React Developers
Building a server action
- [Instructor] We have our app trying to run, but it's not quite working. So if I hit Refresh and click Ask, it's saying that I need to define our getAnswer function. I'm going to do this as a server action. I'm going to create a file called actions.js, and this file is going to be responsible for handling what happens when I make my connection to the API. So let's take a look at what I mean by that. We will first use the directive, use server. So use server signifies that this file contains at least one server action. These are functions that are going to asynchronously run on the server. So we see them used for database calls. We see them used for connecting to APIs like I'm about to do. I'm going to use the generate text function that comes from the AI package, as well as OpenAI from the SDK. So @ai-sdk/openai. From here we will export our asynchronous function, which is called getAnswer. We'll pass a question…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.