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 product

Dynamic default values for product

As we have discussed previously with users and product types, the edit form should display the previous or current data. And for that, we need to fetch the selected product's data and set it as default values in the edit form. So we will create an action to fetch product data using the product ID. Let me open product actions.js file. I will create server action by saying export async function, get unique product and there will be a product ID received as parameter. Inside it, I will use db.FindUnique method to fetch product. So I will write const product is equal to await db.Product.FindUnique. Will add the where clause and the comparison will be id parseIntProductId. Then I want to include product type as well. So I will say include product type true. And now I will return product data by saying return product. Alright, let me save this code. I will navigate to the page jsx file inside the product id folder. And I will destructure params from the props and fetch the product data by…

Contents