0Pricing
React Native Academy · บทเรียน

การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี

กำหนดแนวคิดแอป เลือกแบ็กเอนด์ (Supabase หรือ Firebase) เลือกโครงสร้างการนำทาง ตัดสินใจแนวทางจัดการสถานะ และสร้างโครงร่างโปรเจกต์ด้วยการจัดวางโฟลเดอร์ที่เหมาะสม

การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี เป็นบทเรียน React Native Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน React Native Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Capstone Project Introduction

The capstone is where you apply everything from the React Native track to build a complete, production-ready app from scratch. This lesson covers the critical first step: planning the architecture and tech stack before writing a single line of feature code. Decisions made here — backend, navigation pattern, state management, folder structure — are expensive to change later, so invest time in planning up front.

Defining the App Idea

Start with a clear problem statement: what problem does the app solve and for whom? A well-defined scope prevents feature creep and keeps the project deliverable. Write down: the core value proposition in one sentence, the three to five must-have features for v1, and a list of features explicitly deferred to v2. An app that does three things excellently ships; an app that tries to do everything ships never.

// Example app: 'HabitTracker'

// Problem:
// People struggle to build daily habits because they
// have no lightweight way to track streaks on mobile.

// Core value: Visual streak tracking with daily check-ins

// V1 features:
// 1. Create/delete habits with name and icon
// 2. Mark habit as done each day
// 3. View streak count and calendar history
// 4. Push reminder notification at chosen time
// 5. Basic stats: longest streak, completion rate

// Deferred to V2:
// - Social sharing, friend challenges, premium themes

Choosing a Backend

For a solo developer or small team, Supabase and Firebase are the top BaaS (Backend as a Service) choices. Supabase offers a real PostgreSQL database with SQL queries, row-level security, and excellent TypeScript types. Firebase (Firestore) is document-based with simpler real-time setup but weaker typing. For complex relational data (with joins), choose Supabase. For hierarchical, flexible document data (like chat messages), Firebase is natural. Both provide auth, storage, and functions.

// Decision matrix:

// Supabase:
// + Postgres SQL — complex queries and relationships
// + TypeScript generated types from schema
// + Open source — can self-host
// + Row Level Security (granular per-user access)
// - Slightly more setup for real-time subscriptions

// Firebase:
// + Simpler real-time listeners (onSnapshot)
// + Generous free tier
// + Google's CDN/auth infrastructure
// - NoSQL: no joins, schema migrations harder
// - Vendor lock-in

// For HabitTracker: Supabase (relational habits/completions)

Navigation Structure

Before building screens, design the navigation tree. Most apps have two top-level states: auth screens (login, signup, onboarding — shown when no session exists) and app screens (the main content, shown when authenticated). The auth/app switch is a root navigator. Inside the app state, choose between a tab navigator (main sections) and/or a stack navigator (detail flows). Draw this as a tree before coding.

// HabitTracker navigation tree:

// Root Navigator (stack)
// ├── Auth Stack (shown when no session)
// │   ├── Onboarding Screen
// │   ├── Sign In Screen
// │   └── Sign Up Screen
// └── App Tab Navigator (shown when authenticated)
//     ├── Home Tab (Stack)
//     │   ├── Habit List Screen
//     │   └── Habit Detail Screen
//     ├── Stats Tab (Stack)
//     │   └── Statistics Screen
//     └── Settings Tab (Stack)
//         └── Settings Screen

State Management Decision

Choose state management appropriate to the complexity of your app. A small to medium app works well with React Context + useState for global state (auth session, theme) plus React Query for server data. Larger apps with complex cross-cutting state benefit from Zustand (simple, minimal boilerplate) or Redux Toolkit (powerful, with devtools). Avoid choosing Redux for a small app — the overhead is not justified until you have many slices of interconnected state.

// HabitTracker state management plan:

// Authentication state:
// → Context API (AuthContext with session/user)

// Server data (habits, completions):
// → React Query (useQuery, useMutation)
//   - Caches habits list
//   - Invalidates cache on mutation
//   - Works offline with persistence

// UI state (selected date, modal open):
// → Local useState in each component

// Theme preference (light/dark):
// → Context + AsyncStorage for persistence

// No Redux needed — RQ handles server state well

Folder Structure

A well-organized folder structure makes the codebase navigable as it grows. A common React Native convention: src/ contains everything, with subfolders for screens/, components/ (shared UI), hooks/ (custom hooks), services/ (API/backend calls), store/ or context/ (state), navigation/ (navigators), and utils/ (pure functions). Group by feature for larger apps, by type for smaller apps.

// Recommended folder structure:
src/
  screens/
    auth/
      SignInScreen.tsx
      SignUpScreen.tsx
    habits/
      HabitListScreen.tsx
      HabitDetailScreen.tsx
    stats/
      StatsScreen.tsx
  components/
    HabitCard.tsx
    StreakBadge.tsx
    CheckInButton.tsx
  hooks/
    useHabits.ts
    useCompletions.ts
    useAuth.ts
  services/
    supabase.ts
    habitsApi.ts
  navigation/
    RootNavigator.tsx
    AppTabNavigator.tsx
  context/
    AuthContext.tsx
    ThemeContext.tsx

Database Schema Design

Design your database schema before writing any queries. For Supabase (PostgreSQL), think in tables and relationships. For the HabitTracker app, you need: a habits table (owned by a user), a completions table (one row per habit per day it was checked in), and use Supabase Auth's built-in auth.users table for users. Row Level Security (RLS) policies ensure users can only see their own data.

-- HabitTracker schema (PostgreSQL / Supabase)

CREATE TABLE habits (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  icon TEXT DEFAULT 'star',
  color TEXT DEFAULT '#007AFF',
  reminder_time TIME,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE completions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  habit_id UUID REFERENCES habits(id) ON DELETE CASCADE,
  completed_date DATE NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(habit_id, completed_date)
);

-- RLS policies: users see only their own habits
ALTER TABLE habits ENABLE ROW LEVEL SECURITY;
CREATE POLICY 'Users can CRUD own habits' ON habits
  FOR ALL USING (auth.uid() = user_id);

Scaffolding the Project

With the plan in hand, scaffold the Expo project and immediately configure the essentials: TypeScript, ESLint, Prettier, folder structure, and git. Install your chosen libraries before writing any feature code. Setting up formatting and linting early prevents a messy codebase from accumulating — it is much harder to apply to 1000 lines of existing code than to enforce from line 1.

# Create project with TypeScript template
npx create-expo-app HabitTracker --template expo-template-blank-typescript

cd HabitTracker

# Install core dependencies
npx expo install @react-navigation/native @react-navigation/bottom-tabs \
  @react-navigation/stack react-native-screens react-native-safe-area-context

npx expo install @supabase/supabase-js @react-native-async-storage/async-storage

npm install @tanstack/react-query axios

# Linting and formatting
npm install -D eslint eslint-config-expo prettier

# Initialize git
git init && git add -A && git commit -m 'Initial scaffold'

Environment Variables and Configuration

Set up environment variables before writing any code that touches secrets. Create a .env file for local development and add it to .gitignore immediately. Use EXPO_PUBLIC_ prefix for values that are safe to include in the client bundle (public API keys), and store truly secret values (service role keys) in EAS Secrets only. Never commit any API key or secret to git.

// .env (create this, add to .gitignore)
EXPO_PUBLIC_SUPABASE_URL=https://xyzcompany.supabase.co
EXPO_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5...

// src/services/supabase.ts
import { createClient } from '@supabase/supabase-js';
import AsyncStorage from '@react-native-async-storage/async-storage';

const supabaseUrl = process.env.EXPO_PUBLIC_SUPABASE_URL!;
const supabaseAnonKey = process.env.EXPO_PUBLIC_SUPABASE_ANON_KEY!;

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  auth: {
    storage: AsyncStorage,
    autoRefreshToken: true,
    persistSession: true,
  },
});

Defining TypeScript Types

Define your core TypeScript types in a src/types/ folder before writing components or services. Types for your database tables (Habit, Completion), navigation props (screen parameter lists), and API responses create a shared contract between all parts of the codebase. Supabase CLI can auto-generate types from your schema with supabase gen types typescript, saving significant manual effort.

// src/types/index.ts
export interface Habit {
  id: string;
  user_id: string;
  name: string;
  icon: string;
  color: string;
  reminder_time: string | null;
  created_at: string;
}

export interface Completion {
  id: string;
  habit_id: string;
  completed_date: string; // YYYY-MM-DD
  created_at: string;
}

// Navigation param list
export type AppStackParamList = {
  HabitList: undefined;
  HabitDetail: { habitId: string };
  CreateHabit: undefined;
};

// Generate from Supabase schema:
// npx supabase gen types typescript --project-id <id> > src/types/supabase.ts

Planning Feature Development Order

Build features in dependency order: auth before data screens (data screens need a user ID), navigation shell before individual screens, API layer before UI (so you can test data flow before styling), and core feature before polish (streaming + design). A good first-week plan: Day 1 — auth flow; Day 2 — navigation skeleton; Day 3-4 — habits CRUD; Day 5 — completions and streak logic; Day 6-7 — notifications and polish.

// HabitTracker development order:

// Sprint 1 (Foundation):
// 1. Auth screens + Supabase auth
// 2. Root navigator (auth/app switch)
// 3. Tab navigator + placeholder screens
// 4. Supabase client + React Query setup

// Sprint 2 (Core Feature):
// 5. Habits API (create, read, delete)
// 6. HabitList screen + HabitCard component
// 7. Completions API (toggle check-in)
// 8. Streak calculation logic

// Sprint 3 (Polish):
// 9. Calendar view for habit history
// 10. Stats screen
// 11. Push notifications setup
// 12. Animations on check-in

Quick Check

Test your understanding of React Native Mobile Development concepts from this lesson.

Lesson Recap

In this lesson you learned: how to define a clear app scope and defer non-essential features to v2, how to choose a backend, navigation pattern, and state management approach based on app requirements, and how to design a database schema and folder structure before writing feature code. You also saw how to scaffold the project with the right dependencies and types from day one. Next up we implement the authentication flow and protected routes.

คำถามที่พบบ่อย

บทเรียน “การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส React Native Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส React Native Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี”

กำหนดแนวคิดแอป เลือกแบ็กเอนด์ (Supabase หรือ Firebase) เลือกโครงสร้างการนำทาง ตัดสินใจแนวทางจัดการสถานะ และสร้างโครงร่างโปรเจกต์ด้วยการจัดวางโฟลเดอร์ที่เหมาะสม คุณปฏิบัติ React Native Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน React Native Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน React Native Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน React Native Academy นี้ได้ไหม

ได้ บทเรียน React Native Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การวางแผนสถาปัตยกรรมและชุดเทคโนโลยี
  2. ขั้นตอนการยืนยันตัวตนและเส้นทางที่มีการป้องกัน
  3. ฟีเจอร์หลัก: ฟีดข้อมูลพร้อมการรองรับการใช้งานออฟไลน์
  4. ปรับแต่ง ทดสอบ และเผยแพร่ไปยังทั้งสองสโตร์
← กลับไปที่ React Native Academy