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 product rendering using server action

Dynamic product rendering using server action

In the previous lecture, we have created the product listing API in the admin side. Here we need to make sure the admin server is running. Now I will navigate to the client folder and create an actions folder. Here I will create ProductActions.js file and inside it I will say export async function get products. As we are using server action, I will declare the use server directive at the top of the file. Here I will use the fetch API method for fetching the products and as we know it will take the API address as parameter which is going to be the localhost 3001 slash API slash products in our case. But now writing the localhost 3001 every time is not a smart practice. So we will take a global environment variable and store the http colon slash slash localhost 3001 as base URL in the .env that is the environment file. So let me create the .env file and I will create the environment variable base underscore URL is equal to then let me go to the browser and copy the admin URL and paste…

Contents