React Native CLI
Standar pengembangan aplikasi menggunakan React Native CLI (bare workflow) — memberikan kontrol penuh terhadap native modules dan build configuration.
Kapan Menggunakan RN CLI?
| Kriteria | RN CLI | Expo |
|---|---|---|
| Custom native modules | ✅ Penuh | ⚠️ Terbatas (dev client) |
| Native code access | ✅ Langsung | ❌ Via config plugins |
| Build customization | ✅ Penuh | ⚠️ Terbatas |
| Setup complexity | ⚠️ Lebih rumit | ✅ Lebih mudah |
| Upgrade path | ⚠️ Manual | ✅ Otomatis |
Gunakan RN CLI jika:
- Project membutuhkan native modules kustom.
- Integrasi SDK native yang tidak tersedia di Expo.
- Membutuhkan kontrol penuh atas Gradle/Xcode configuration.
Sejak React Native 0.82, legacy Bridge sudah dihapus total — Fabric (renderer), TurboModules, dan JSI kini satu-satunya arsitektur yang tersedia. Native module lama yang masih ditulis dengan Bridge API (NativeModules/NativeEventEmitter gaya lama) harus dimigrasikan ke TurboModules sebelum upgrade. Untuk project baru, generate module native langsung dengan Codegen (create-react-native-library) supaya kompatibel sejak awal.
Tech Stack
| Kategori | Library | Versi Min. |
|---|---|---|
| Runtime | React Native | 0.87+ |
| Architecture | New Architecture (Fabric + TurboModules + JSI) | Wajib sejak 0.82 |
| Language | TypeScript | 5.x |
| Navigation | React Navigation | 7.x |
| Client State | Zustand (Redux Toolkit hanya untuk project lama) | Latest |
| HTTP Client | Axios | Latest |
| Server State | TanStack Query (dipakai bersama Axios) | 5.x |
| Forms | React Hook Form + Zod | Latest |
| UI Library | React Native Paper / NativeWind | Latest |
| Secure Storage | react-native-keychain — token & data sensitif | Latest |
| Local Storage | MMKV — data non-sensitif (preferensi, cache) | Latest |
| Testing | Jest, React Native Testing Library | Latest |
| Linting | ESLint + Prettier | Latest |
| CI/CD | Fastlane + GitLab CI | — |
Project Structure
Struktur React Native mengikuti pola feature-first ala Bulletproof React. Prinsip Clean Architecture (pemisahan data, logic, dan UI) tetap berlaku, tapi diwujudkan sebagai file dan fungsi, bukan class UseCase / RepositoryImpl. Di ekosistem React, peran repository + cache sudah diambil alih oleh TanStack Query, sehingga lapisan class tambahan hanya menambah boilerplate.
src/
├── app/
│ ├── App.tsx # Root component
│ ├── providers.tsx # QueryClientProvider, ThemeProvider, SafeArea, dst
│ └── navigation/ # RootNavigator, linking config
├── features/
│ ├── leave/
│ │ ├── api/
│ │ │ ├── leave.schema.ts # Zod schema respons API (kontrak data)
│ │ │ ├── leave.api.ts # Fungsi fetch + mapping DTO → model
│ │ │ └── useLeaves.ts # Hook TanStack Query (useQuery / useMutation)
│ │ ├── components/ # Komponen khusus fitur ini
│ │ ├── screens/
│ │ │ └── LeaveListScreen.tsx
│ │ ├── hooks/ # Logic UI (useLeaveForm, dst)
│ │ ├── logic/ # Aturan bisnis sebagai fungsi murni (opsional)
│ │ ├── store/ # Zustand — hanya kalau ada client state khusus fitur
│ │ └── types.ts # Model fitur
│ └── attendance/
├── components/ # Shared UI — atoms/, molecules/, organisms/ (Atomic Design)
├── lib/ # apiClient (axios + interceptor), queryClient, secureStorage
├── stores/ # Global client state (session, theme)
├── hooks/ # Shared hooks
├── types/ # Shared types, termasuk tipe param navigasi
├── config/ # Env & konstanta
└── utils/
android/ # Native Android project
ios/ # Native iOS project
Pemetaan ke Clean Architecture
| Layer | Di React Native | Isi |
|---|---|---|
| Data | features/x/api/*.schema.ts, *.api.ts | Validasi respons (Zod), request, mapping DTO → model |
| Domain | features/x/types.ts, features/x/logic/ | Model + aturan bisnis sebagai fungsi murni (mudah di-unit test) |
| Server state | features/x/api/use*.ts | TanStack Query: cache, retry, invalidasi |
| Presentation | features/x/screens/, components/, hooks/ | UI + logic UI |
Aturan Dependensi
- Fitur tidak boleh import fitur lain. Kalau dua fitur butuh hal yang sama, pindahkan ke folder shared (
components/,lib/,types/, dst). Penggabungan beberapa fitur dilakukan diapp/(misalnya di navigator). - Folder shared tidak boleh import
features/atauapp/. features/tidak boleh importapp/. Tipe param navigasi disimpan ditypes/navigation.tssupaya bisa dipakai fitur tanpa importapp/.- Import langsung ke file, tanpa barrel
index.tsdi dalam fitur. Barrel file memperlambat Metro dan mudah menimbulkan circular import.
Aturan 1–3 dijaga ESLint, bukan hanya dengan review:
// eslint.config.js
import importPlugin from 'eslint-plugin-import';
const features = ['leave', 'attendance', 'profile']; // tambahkan setiap fitur baru
export default [
{
plugins: { import: importPlugin },
settings: { 'import/resolver': { typescript: true } },
rules: {
'import/no-restricted-paths': [
'error',
{
zones: [
// 1. Fitur tidak boleh import fitur lain
...features.map((name) => ({
target: `./src/features/${name}`,
from: './src/features',
except: [`./${name}`],
})),
// 2. Shared tidak boleh import features / app
{
target: [
'./src/components',
'./src/lib',
'./src/stores',
'./src/hooks',
'./src/types',
'./src/config',
'./src/utils',
],
from: ['./src/features', './src/app'],
},
// 3. Features tidak boleh import app
{ target: './src/features', from: './src/app' },
],
},
],
},
},
];
Resolver typescript membutuhkan package eslint-import-resolver-typescript supaya path alias (@/...) ikut terbaca.
Coding Standards
TypeScript Strict Mode
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}
Component Pattern
// ✅ Good - typed props langsung, tanpa React.FC
// (React.FC menambahkan `children` secara implisit dan mempersulit generics)
interface UserCardProps {
user: User;
onPress: () => void;
}
export function UserCard({ user, onPress }: UserCardProps) {
return (
<Pressable onPress={onPress} style={styles.container}>
<Text style={styles.name}>{user.name}</Text>
<Text style={styles.email}>{user.email}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
container: { padding: 16, borderRadius: 8 },
name: { fontSize: 16, fontWeight: '600' },
email: { fontSize: 14, color: '#666' },
});
Data Fetching Pattern
// features/leave/api/leave.schema.ts — kontrak respons API
export const leaveDtoSchema = z.object({
id: z.string(),
start_date: z.string(),
reason: z.string().nullable(),
});
export type LeaveDto = z.infer<typeof leaveDtoSchema>;
// features/leave/api/leave.api.ts — request + mapping DTO → model
export async function getLeaves(): Promise<Leave[]> {
const { data } = await apiClient.get('/leaves');
return z.array(leaveDtoSchema).parse(data).map(toLeave);
}
const toLeave = (dto: LeaveDto): Leave => ({
id: dto.id,
startDate: new Date(dto.start_date),
reason: dto.reason ?? '',
});
// features/leave/api/useLeaves.ts — server state via TanStack Query
export const leaveKeys = {
all: ['leaves'] as const,
};
export function useLeaves() {
return useQuery({ queryKey: leaveKeys.all, queryFn: getLeaves });
}
// features/leave/screens/LeaveListScreen.tsx
export function LeaveListScreen() {
const { data: leaves = [], isPending, error, refetch } = useLeaves();
if (isPending) return <LeaveListSkeleton />;
if (error) return <ErrorView error={error} onRetry={refetch} />;
if (leaves.length === 0) return <EmptyState />;
return <LeaveList leaves={leaves} />;
}
queryFnTanStack Query memanggil queryFn dengan object context ({ queryKey, signal, ... }). Kalau fungsi butuh parameter, bungkus secara eksplisit: queryFn: () => getLeave(id), jangan queryFn: getLeave.
Build & Run
# Install dependencies
npm install
# iOS pod install
cd ios && pod install && cd ..
# Run Android
npx react-native run-android
# Run iOS
npx react-native run-ios
# Run tests
npm test
# Lint
npx eslint src/ --fix
# Build Android release
cd android && ./gradlew assembleRelease
# Build iOS release (via Fastlane)
cd ios && fastlane release
Setelah menambahkan library yang memiliki native module, wajib jalankan cd ios && pod install dan rebuild.