From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Dynamic default values for edit

Dynamic default values for edit

Now, we need to have the selected user's data filled as default values in the edit form. So we need to have dynamic default values. And for that, we need to fetch the specific user's data from the database. So we will create an action to fetch user data using the user ID. So in the userAction.js file, I'll create a server action export const getUniqueUser is equal to async will receive the userId as parameter here in the arrow function and then run the db.findUnique method to fetch the user. So I will say const user is equal to await db dot admin user dot find unique. Let's define the where clause where I will say id is equal to user id. And now we will return the user data. So we'll say return user. All right, now in the screen component, I'll call the server and fetch the data and then set that fetched data as default value for all the input fields. I will take a constant and parse the user id into an integer, because we need to parse the integer id, so I will say const id is equal…

Contents