Skip to content

EmeryCSI/CSI-246W25-Week4-DataFetching

Repository files navigation

Data Fetching Tutorial App

This project is a tutorial application demonstrating various data fetching techniques in a Next.js environment. We explore three different methods to fetch and manage data within a Next.js application:

  1. Simple Server Function Call: Fetching data directly within a server component.
  2. Client-Side Fetch to an API Route: Using React hooks to fetch data from an API route.
  3. Server Action: Utilizing Next.js Server Actions for direct server-side mutations.

Techniques

1. Simple Server Function Call

In this approach, data is fetched directly within a server component. This method is ideal for initial page loads where data is required before the page reaches the client.

  • File: app/posts/server-posts/page.tsx
  • Description: Fetches data on the server during component render.

2. Client-Side Fetch to an API Route

This method uses React hooks like useState and useEffect to fetch data from an API route after the component mounts in the browser. It supports dynamic updates and user interactions.

  • File: app/posts/client-posts/page.tsx
  • Description: Fetches data from an API route using client-side code.

3. Server Action

Next.js Server Actions enable direct server-side mutations without needing separate API routes. This approach provides excellent progressive enhancement as forms can work even without JavaScript enabled.

  • File: app/posts/server-actions/page.tsx
  • Description: Uses Server Actions for form submissions and data mutations.

Getting Started

To run this project locally, follow these steps:

  1. Clone the repository:

    git clone <repository-url>
  2. Install dependencies:

    npm install
  3. Run the development server:

    npm run dev
  4. Open your browser and navigate to http://localhost:3000.

Project Structure

  • app/posts/server-posts/page.tsx: Demonstrates server-side data fetching.
  • app/posts/client-posts/page.tsx: Demonstrates client-side data fetching.
  • app/posts/server-actions/page.tsx: Demonstrates server actions for data mutations.
  • app/api/posts/route.ts: API route for handling posts.
  • app/lib/db.ts: Simulated database functions for posts.

Conclusion

This tutorial app provides a comprehensive overview of different data fetching techniques in Next.js. Each method has its own use cases and benefits, allowing you to choose the best approach for your specific needs.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors