Iranzi Thierry Avatar
HomeProjectsResumeArticles

© 2025 @iranzithierry. All rights reserved.

  • Home
  • Projects
  • Resume
  • Articles

Understanding the T3 Stack

December 26, 2024 (7mo ago)

Understanding the T3 Stack

The T3 Stack is a modern full-stack development framework designed to help developers create robust and scalable applications. It combines powerful tools and libraries to streamline both front-end and back-end development while maintaining a strong focus on type safety and developer experience.


What is the T3 Stack?

The T3 Stack is an opinionated stack of technologies built on top of Next.js. It integrates a set of best-in-class tools to offer a cohesive and efficient development experience. The stack emphasizes:

  1. Type safety: Using TypeScript across the entire stack.
  2. Full-stack capabilities: Seamless integration of front-end and back-end tools.
  3. Developer experience: Easy-to-use, well-documented tools.

Core Components of the T3 Stack

  1. Next.js:

    • A React framework that supports server-side rendering, static site generation, and API routes.
    • Handles front-end rendering and routing with powerful server-side capabilities.
  2. tRPC (TypeScript Remote Procedure Calls):

    • Enables type-safe API communication between the front-end and back-end without REST or GraphQL.
    • Simplifies server-client interaction with auto-completion and type inference.
  3. Prisma:

    • A modern ORM for type-safe database interactions.
    • Works seamlessly with relational databases and supports migrations, data modeling, and querying.
  4. Tailwind CSS:

    • A utility-first CSS framework for building responsive and customizable user interfaces.
    • Enhances productivity with pre-configured design tokens and an intuitive class system.
  5. TypeScript:

    • A superset of JavaScript that adds static typing, ensuring type safety across the entire stack.

Optional Components

  • NextAuth.js:

    • Provides authentication and session management for Next.js applications.
    • Supports OAuth, email/password, and custom authentication methods.
  • Zod:

    • A TypeScript-first schema validation library.
    • Ensures data validation and type safety.

Why Choose the T3 Stack?

The T3 Stack is ideal for developers who want to:

  1. Build scalable full-stack applications:

    • Leverage Next.js’s server-side capabilities and tRPC’s type-safe APIs.
  2. Prioritize developer experience:

    • Get access to type safety, powerful tools, and robust libraries.
  3. Focus on type safety:

    • Achieve end-to-end type safety from the database to the front-end UI.
  4. Reduce complexity:

    • Simplify the development workflow by combining well-integrated tools.

Getting Started with the T3 Stack

Step 1: Create a New T3 Stack Application

Use the official CLI tool provided by T3:

npm create t3-app@latest

Follow the prompts to configure your application with the desired features (e.g., Prisma, Tailwind CSS, NextAuth.js).

Step 2: Set Up Your Environment

  1. Install dependencies:
npm install
  1. Configure the .env file with your environment variables (e.g., database connection string).

Step 3: Run the Development Server

Start the development server to preview your application:

npm run dev

Example Application with the T3 Stack

Here’s a simple example of a T3 Stack application:

1. Database Model with Prisma

Define a User model in prisma/schema.prisma:

model User {
  id    Int    @id @default(autoincrement())
  name  String
  email String @unique
}

Run migrations:

npx prisma migrate dev --name init

2. Create a tRPC Router

Set up a tRPC router to fetch users:

// src/server/routers/user.ts
import { createRouter } from "../trpc";
import { z } from "zod";
 
export const userRouter = createRouter()
  .query("getAll", {
    async resolve({ ctx }) {
      return await ctx.prisma.user.findMany();
    },
  });

3. Fetch Data in a Page

Use tRPC to fetch and display users:

import { trpc } from "../utils/trpc";
 
export default function Users() {
  const { data: users, isLoading } = trpc.user.getAll.useQuery();
 
  if (isLoading) return <p>Loading...</p>;
 
  return (
    <div>
      <h1>Users</h1>
      <ul>
        {users?.map((user) => (
          <li key={user.id}>{user.name} ({user.email})</li>
        ))}
      </ul>
    </div>
  );
}

Conclusion

The T3 Stack is a powerful and opinionated framework for building modern, type-safe, full-stack applications. Its components work seamlessly together, making it a go-to choice for developers who value efficiency, scalability, and type safety.