Skip to content
Back to Blog

July 13, 2023

React Native ile MongoDB: 2026 Rehberi

React Native uygulamanızda MongoDB kullanmak mı istiyorsunuz? İşte 2026 için güncel yöntem.

Neden MongoDB?

  • Esnek şema - veri yapısı değişince migration yok
  • Gerçek zamanlı senkronizasyon
  • Offline-first desteği
  • Ölçeklenebilirlik

Kurulum

2026'da doğru yol: @realm/react

npm install @realm/react realm

Temel Kullanım

1. Schema Tanımla

import Realm from 'realm';class Task extends Realm.Object { _id!: Realm.BSON.ObjectId; title!: string; isComplete!: boolean; static schema = { name: 'Task', primaryKey: '_id', properties: { _id: 'objectId', title: 'string', isComplete: { type: 'bool', default: false }, }, };}

2. Provider Ekle

import { RealmProvider } from '@realm/react';function App() { return ( <RealmProvider schema={[Task]}> <TaskList /> </RealmProvider> );}

3. Veri Oku/Yaz

import { useRealm, useQuery } from '@realm/react';function TaskList() { const realm = useRealm(); const tasks = useQuery(Task); const addTask = () => { realm.write(() => { realm.create('Task', { _id: new Realm.BSON.ObjectId(), title: 'Yeni görev', }); }); }; return ( <View> <FlatList data={tasks} renderItem={({ item }) => <Text>{item.title}</Text>} /> <Button title="Ekle" onPress={addTask} /> </View> );}

MongoDB Atlas ile Senkronizasyon

Cloud'a bağlanmak için:

1. Atlas App Services oluştur

  • mongodb.com/cloud/atlas adresinden
  • App Services → Create Application

2. Flexible Sync aktifleştir

import { AppProvider, UserProvider } from '@realm/react';function App() { return ( <AppProvider id="your-app-id"> <UserProvider fallback={<Login />}> <RealmProvider schema={[Task]} sync={{ flexible: true, onError: console.error, }} > <TaskList /> </RealmProvider> </UserProvider> </AppProvider> );}

Best Practices

Offline-First

  • Realm otomatik olarak offline çalışır
  • İnternet gelince senkronize olur

Performans

  • useQuery ile otomatik re-render
  • Lazy loading varsayılan

Güvenlik

  • Atlas App Services'ta Rules tanımla
  • Kullanıcı bazlı erişim kontrolü

Sonuç

2026'da React Native + MongoDB için doğru yol:

  • @realm/react paketi
  • Atlas App Services
  • Flexible Sync

Eski mongodb-stitch-react-native deprecated, kullanmayın.

Link: realm.io/docs/sdk/react-native