react-native-nitro-fetch is a general purpose network fetching library for React Native. It can be used as a drop-in replacement for the built-in fetch(...) method, as well as provide additional features like prefetching and workletized mappers.
This can be used as a drop-in-replacement for the built-in fetch(...) method.
Prefetching in JS
You can prefetch a URL in JS, which keeps the result cached for the next actual fetch(...) call - this can be used shortly before navigating to a new screen to have results hot & ready:
Prefetching data on app launch (or process start) will make it hot & ready once your JS code actually runs. Call prefetchOnAppStart(...) to enqueue a prefetch for the next app start:
In our tests, prefetching alone yielded a ~220 ms faster TTI (time-to-interactive) time! 🤯
Token refresh (cold start)
When you use auto-prefetch (prefetchOnAppStart) and/or WebSocket prewarm on app start (react-native-nitro-websockets), native code runs before your JS bundle. If those requests need auth headers, you can register a token refresh configuration. On each cold start, native code calls your refresh URL, maps the response into HTTP headers, and merges them into auto-prefetches and/or WebSocket prewarms.
1. Register the refresh config (persisted in encrypted native storage):
import { registerTokenRefresh } from'react-native-nitro-fetch'registerTokenRefresh({
target: 'fetch', // 'websocket' | 'fetch' | 'all'url: 'https://api.example.com/oauth/token',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ grant_type: 'client_credentials' }),
responseType: 'json',
mappings: [
{ jsonPath: 'access_token', header: 'Authorization', valueTemplate: 'Bearer {{value}}' },
],
// If the refresh request fails:// - 'useStoredHeaders' — use last successful headers from the previous run (default)// - 'skip' — skip auto-prefetch / prewarm entirely when refresh failsonFailure: 'useStoredHeaders',
})
Response mapping
Default responseType is 'json'. Use mappings to copy fields from the JSON body into header names (dot paths supported, e.g. data.token).
Use compositeHeaders to build a header from a template and multiple JSON paths ({{placeholder}} in the template).
For a plain-text body, set responseType: 'text' and use textHeader / optional textTemplate (with {{value}}).
Example: token refresh + WebSocket prewarm
import { registerTokenRefresh } from'react-native-nitro-fetch'import { prewarmOnAppStart, NitroWebSocket } from'react-native-nitro-websockets'constWSS = 'wss://api.example.com/live'registerTokenRefresh({
target: 'websocket', // use 'all' if you also use prefetchOnAppStart with the same token flowurl: 'https://api.example.com/oauth/token',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
grant_type: 'client_credentials',
client_id: '…',
client_secret: '…',
}),
mappings: [
{ jsonPath: 'access_token', header: 'Authorization', valueTemplate: 'Bearer {{value}}' },
],
})
3. Optional JS helpers
import {
callRefreshEndpoint,
clearTokenRefresh,
getStoredTokenRefreshConfig,
} from'react-native-nitro-fetch'// Same mapping rules as native; uses global fetch from JSconst headers = awaitcallRefreshEndpoint(config)
// Remove stored config and token caches (scope with 'fetch' | 'websocket' | 'all')clearTokenRefresh('fetch')
// Read back what was registered (or null)const stored = getStoredTokenRefreshConfig('fetch')
The refresh config and header caches are stored with platform secure storage (Android Keystore + encrypted values in SharedPreferences, iOS Keychain-backed encryption in the same UserDefaults suite as other nitro keys).
AbortController
Cancel in-flight requests using the standard AbortController API:
import { fetch } from'react-native-nitro-fetch'const controller = newAbortController()
// Abort after 500mssetTimeout(() => controller.abort(), 500)
try {
const res = awaitfetch('https://httpbin.org/delay/20', {
signal: controller.signal,
})
} catch (e) {
if (e.name === 'AbortError') {
console.log('Request was cancelled')
}
}
Pre-aborted signals are also supported — the request will throw immediately without making a network call:
Nitro Fetch can also expose an streaming mode that returns a ReadableStream body.
Combined with react-native-nitro-text-decoder, you can incrementally decode UTF‑8 chunks:
import { useRef, useState } from'react'import { fetch as nitroFetch } from'react-native-nitro-fetch'import { TextDecoder } from'react-native-nitro-text-decoder'exportfunctionStreamingExample() {
const [output, setOutput] = useState('')
const decoder = useRef(newTextDecoder())
constappend = (text: string) => {
setOutput(prev => prev + text)
}
construnStream = async () => {
// `stream: true` enables the streaming transportconst res = awaitnitroFetch('https://httpbin.org/stream/20', {
stream: true,
})
const reader = res.body?.getReader()
if (!reader) {
append('No readable stream!')
return
}
let chunks = 0while (true) {
const { done, value } = await reader.read()
if (done) break
chunks++
const text = decoder.current.decode(value, { stream: true })
append(text)
}
append(`\n\n✅ Done — ${chunks} chunk(s) received`)
}
// Call `runStream()` from a button handler in your UI
}
WebSockets & prewarm
Use react-native-nitro-websockets for NitroWebSocket (browser-like API: onopen, onmessage, send, close, …). Install react-native-nitro-text-decoder alongside it — the socket package uses it to decode UTF-8 text frames.
Prewarm on next launch — queue URLs from JS so native code can start the handshake before React loads:
On Android, call NitroWebSocketAutoPrewarmer.prewarmOnStart(this) in Application.onCreate (see example MainApplication.kt). iOS picks up the queue via the linked pod.
Authenticated prewarms: use registerTokenRefresh with target: 'websocket' or 'all' — see Token refresh (cold start) for a small registerTokenRefresh + prewarmOnAppStart + NitroWebSocket example.
WebSockets are not part of react-native-nitro-fetch itself; use the companion package react-native-nitro-websockets (with react-native-nitro-text-decoder). For other stacks, react-native-fast-io is another option.
The npm package react-native-nitro-text-decoder receives a total of 54,320 weekly downloads. As such, react-native-nitro-text-decoder popularity was classified as popular.
We found that react-native-nitro-text-decoder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 14 May 2026
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
The White House’s Gold Eagle Initiative aims to coordinate AI-discovered vulnerabilities, validate findings, and accelerate patching across critical software.