I did not set out to build an npm package. I was building an app for users in Central Africa where mobile data is expensive and connections are unreliable. The app needed to display country flags. I reached for the simplest solution and immediately noticed something I could not ignore.
The problem that started it
The simplest way to display a country flag in React Native is a Text component with a flag emoji. It works, it is instant, and it requires no network. But flag emoji rendering is inconsistent across Android versions — some devices show vivid colored glyphs, others show black-and-white letter pairs. For a production app, the inconsistency felt unprofessional.
The alternative is SVG flags from a CDN. They look identical everywhere and have consistent dimensions. But the naive implementation looks like this:
This fires a network request every single time the component mounts. On a screen that lists 20 countries, that is 20 network requests. On a poor connection, each one adds visible latency. On a metered data plan, each one costs the user money.

I added AsyncStorage caching in about thirty lines. Check the cache first, fetch if missing, store the result. The component became instant after the first load. I used it in my app and moved on.
Two weeks later I was cleaning up the codebase and realized I had copy-pasted the same caching logic into three different screens. That is the moment I decided to make it a package.
The first version — just make it work
The initial scope was deliberately narrow. One component, one behavior: check AsyncStorage, fetch if not found, render the SVG.
The first real decision was the CDN. I had been using flagcdn.com but the
flag dimensions there are inconsistent — some countries have unusual aspect
ratios and flags placed side by side do not align. I switched to
flagicons.lipis.dev which enforces a strict 4:3 ratio across every country.
That one change improved every screen that showed multiple flags together.
The component API at v0.0.1 was minimal:
Emoji mode became the default because it requires nothing — no network, no AsyncStorage, no peer dependencies. A developer who just wants flags on a non-critical screen should not pay the cost of SVG infrastructure.
I published v0.0.1. A few days later I noticed the first thing I had gotten wrong.
The silent caching bug
When a flag fetch failed — say, a network timeout or a 404 for an invalid country code — my original code did this:
It stored the grey placeholder SVG permanently under the real flag's cache key. On the next render, the cache lookup would succeed and return the placeholder. The real flag would never load, even after the user's connection was restored.
This is the kind of bug that is invisible in development because your local internet never goes down. It only surfaces in production on unreliable connections — exactly the users this package was built for.
The fix was simple: stop caching failures. A failure means something went wrong with the network or the resource. The correct behavior is to leave the cache empty and try again next time.
This went into v0.2.0 alongside the aspectRatio prop and the useFallbackEmoji
offline behavior. The offline story now had three states: loading placeholder,
dashed border placeholder (uncached and offline), and emoji fallback.

The deduplication problem
The bug that embarrassed me most was one I only found by building a demo screen.
I had a country picker in the example app — a scrollable list of 250 countries, each with a flag. When it mounted, I opened the network inspector. Twelve of the same flag had been requested twelve times simultaneously.
The cause was obvious in retrospect. Each CountryFlag instance runs its own
useEffect. If twelve components mount in the same render cycle, all twelve
execute the effect simultaneously. The cache check is asynchronous — by the
time the first component reads from AsyncStorage and finds nothing, the other
eleven have already started their own fetches.
The standard solution for this is an in-flight request map: a module-level
Map that tracks promises currently in flight. Before starting a new fetch,
check the map. If there is already a promise for this key, return it instead
of starting another.
The critical detail is registering the promise in the map before the first
await. If you register after, a second call arriving in the same microtask
tick will not see it and will start a duplicate fetch anyway.


This shipped in v1.0.0 alongside two other features I had been holding back:
onLoad/onError callbacks and preloadFlags — a utility to warm the cache
before a screen renders, useful for onboarding flows and country pickers.
The cache key double-building bug
This one took me longer to find than it should have.
After the deduplication work I had refactored the caching layer to use a
buildCacheKey helper. The intent was to ensure keys were built consistently
everywhere. But I introduced a subtle mistake:
In fetchFlag, I was calling buildCacheKey to construct the key for the
deduplication map, then passing that pre-built key to getCachedFlag as if
it were a raw isoCode. Inside getCachedFlag, the function called
buildCacheKey again — on a string that was already a full key.
The result was keys like @rn_cached_flags_@rn_cached_flags_cm_4x3_4x3.
Nothing ever matched. Every render fetched from the network. The cache was
completely broken and I had not noticed because my test environment always
had flags preloaded.
The fix was straightforward: fetchFlag passes the raw lowerCode to
getCachedFlag, and lets the cache layer build its own key. The deduplication
map continues to use a locally-built key for its own lookups. Each layer owns
its key construction.
I also found a second bug in the same pass: clearAllFlagVariants was doing
a case-sensitive string match against keys that were stored in lowercase. Passing
"CM" would never match @rn_cached_flags_cm_4x3. The fix was a single
.toLowerCase() call before the filter.
Both bugs went into v1.1.0.
Removing the only dependency
The package had one runtime dependency: country-code-to-flag-emoji. It did
one thing — convert a two-letter country code into the corresponding flag emoji.
Flag emojis in Unicode are not stored as individual characters. They are
constructed from pairs of regional indicator symbols — a set of 26 code points
from U+1F1E6 (🇦) to U+1F1FF (🇿). Two regional indicators placed side by
side combine into a flag emoji on supported platforms. The entire implementation
is arithmetic:
That is the core. The surrounding code handles input normalization — extracting
country codes from IETF language tags like en-US and ISO subdivision codes
like GB-SCT. The full implementation is about 60 lines.
Removing the external dependency reduced the package size and eliminated a
dependency update vector. More importantly, it let me extend the API surface
in a way the original package did not support — accepting en-US and GB-SCT
directly without any pre-processing by the consumer.
I also added getFlagUrl in the same release — a pure function that returns
the CDN URL for a given country code. No caching, no React, no
react-native-svg. Just a URL. This covered the web and Next.js use cases
without any changes to the component internals.
Changing the cache storage format
Up to v0.x, the cache stored raw SVG strings:
This made TTL impossible — there was no way to know when a value was stored. In v1.0.0 I changed the format to a JSON payload:

The TTL check then becomes simple arithmetic on read:
The migration required backward compatibility. Users upgrading from v0.x had
existing raw SVG strings in their AsyncStorage. The new reader handles this
with a try/catch — if JSON.parse fails, the value is a legacy raw string and
is returned as-is. On next write, it will be saved in the new format.
The global config system
By v1.x the component had a reasonable number of props. For an app that
displays flags in many places — a phone input, a country picker, a profile
screen, a registration form — setting the same cacheTTLDays, aspectRatio,
and useFallbackEmoji on every instance was repetitive.
The solution is the same pattern used by react-query, axios, and most
well-designed libraries: a provider at the app root that sets defaults, with
per-instance props that override them.
The resolution chain has four levels:

The provider is fully opt-in. Every existing usage continues to work without changes. The only thing that changes is that per-instance props now read from the context when not explicitly set.
The configureCachedFlags() function handles the case where utilities like
preloadFlags and getFlagUrl are called outside the React tree — in a
splash screen, in a background task, or at app startup before any component
mounts. It sets the same config object the provider reads from, so both
surfaces stay in sync.
I also added cdnBaseUrl as a configurable option at this point. The default
CDN is public and works for most cases, but enterprise apps sometimes need
to serve assets from their own infrastructure. A single config option handles
this without any changes to individual components.
What I learned about shipping open source
The first version took a weekend. Getting it right took four months and eight releases.
The bugs I found were not the ones I anticipated. I expected to deal with edge cases in the SVG rendering or the AsyncStorage API. Instead the hardest problems were in the coordination layer — the timing of async operations, the contracts between functions that both dealt with cache keys, the behavior when the same component is rendered N times simultaneously.
These are not bugs you find by reading your own code carefully. You find them by using the package in a real project, building a demo screen that exercises an edge case, and watching something behave wrong. Every significant fix in this package came from one of those moments.

The other thing I learned: the README is a product. The first version was a props table and a code snippet. Downloads picked up noticeably after I rewrote it to explain the caching flow, show the offline behaviour table, and document the deduplication guarantee. Developers do not install packages they do not understand. The README is where understanding happens.

The package is currently at v2.0.0. It has zero runtime dependencies, a clean API surface, and handles every edge case I have found in real usage. I am not planning a v3 — at some point a focused package should stop growing and start being used.
The source is at github.com/SiandjaRemy/react-native-cached-flags. If you build React Native apps that show country flags, try it. If something does not work the way you expect, open an issue — that is how most of the good features in this package came to exist.
react-native-cached-flags is available on npm. Install it with npm install react-native-cached-flags or yarn add react-native-cached-flags. Peer
dependencies: react-native-svg and
@react-native-async-storage/async-storage.
