Creating a Client
Understand the differences between using the factory function createClient() and the pre-instantiated default client singleton.
1. Factory Function: createClient() (Recommended)
The factory pattern is ideal for modern applications because it allows you to create multiple isolated instances with distinct base URLs, authentication tokens, caching rules, and timeouts:
src/api.ts
typescript
import { createClient } from 'react-native-fetch-client';
// Client A: Authenticated user API
export const userApi = createClient({
baseURL: 'https://api.app.com/v1',
timeout: 10000,
bearerToken: async () => await getSessionToken(),
});
// Client B: Public CDN or Analytics
export const analyticsApi = createClient({
baseURL: 'https://telemetry.app.com',
timeout: 5000,
retry: { attempts: 1 },
});2. Default Singleton Instance
If you are building a quick script or only have a single backend, you can import the default client directly. It comes with a .create() method attached, providing 100% API compatibility with axios.create():
typescript
import client from 'react-native-fetch-client';
// 1. Direct one-line fetch without configuration:
const res = await client.get('https://api.github.com/users/octocat');
console.log(res.data);
// 2. Factory method on the default export (identical to axios.create()):
const custom = client.create({ baseURL: 'https://api.example.com' });Thread & Memory Safety
Each client instance created by
createClient() maintains its own isolated in-memory cache, dedup queue, and interceptor managers. They will never leak state across different client instances.