Introduction
In this article, I am gonna summarize the modes about data fetching in NextJs for better understanding of NextJs.
API Router Mode
Tranditionally,we use fetch or axios tool to request data by HTTP-Request.
This mode, meanwhile, is supposed to offer api to outside world in public.
However, when you use fetch-api in Server Component,that obviously is inapproriate. Because why fecthing api in the same place. In Server Component, we can directly call the function service from server.
Server Component Call Service Mode
In Server Component, we can call the service function from server, at the same time,remain the api ability in public.
Client Component Call Service Mode
In Client Component, we can't call the service function from server directly. because, the service does not exist in Client Side. But, we can continue to use api router mode.
If we want to do this, we should mark service file as "use server"