ExpoIntermediate
REST API থেকে data — fetch ও POST
নিজের mock API-তে task sync — fetch-এর সঠিক নিয়ম।
4টি অংশবাংলা ভাষায়
সার্ভার-এ task জমা/আনা — native `fetch`-ই যথেষ্ট:
lib/api.tsts
ts
const API_URL = "https://api.example.com";
export async function fetchTasks() {
const res = await fetch(API_URL + "/tasks");
if (!res.ok) throw new Error("Fetch failed");
return await res.json();
}
export async function createTaskOnServer(task) {
const res = await fetch(API_URL + "/tasks", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(task),
});
if (!res.ok) throw new Error("Create failed");
return await res.json();
}res.okcheck — 2xx-এর বাইরে error throw- timeout —
AbortControllerদিয়ে - network error — catch-এ দেখান
localhost ভুলবে না
ফিজিক্যাল ফোনে localhost মানে ফোন নিজেই — আপনার কম্পিউটারের LAN IP (http://192.168.x.x:3000) ব্যবহার করুন; Android emulator-এ 10.0.2.2।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।