MediumMicrofrontends📖 Theory Question

How do you share state between microfrontends?

💡

Hint

No shared React tree — use Custom Events, a shared pub/sub singleton, URL state, or a backend-for-frontend

Full Answer

Microfrontends run in separate JavaScript scopes with no shared component tree. State sharing options:

1. Custom DOM Events — the simplest approach. Fire a CustomEvent on window; other apps listen.

// App A fires
window.dispatchEvent(new CustomEvent('cart:updated', { detail: { count: 3 } }));

// App B listens
window.addEventListener('cart:updated', (e) => setCartCount(e.detail.count));

2. Shared event bus singleton — expose a pub/sub module via Module Federation's shared config. All apps import the same instance.

3. URL / query params — state that should survive navigation and deep links belongs in the URL. React Router's search params work across app boundaries.

4. Web StoragelocalStorage / sessionStorage with storage events for cross-tab sync. Simple but no reactive binding.

5. Backend-for-Frontend (BFF) — the authoritative state lives server-side. Each MFE re-fetches when needed. No frontend coupling at all — preferred for complex state.

💡 The cleanest architecture avoids shared frontend state entirely — each MFE is responsible for its own data and communicates only through well-defined events or the URL.

More Microfrontends Questions

EasyWhat is a microfrontend and what problem does it solve?EasyHow does Webpack Module Federation work?EasyWhat are the main challenges of microfrontends in production?EasyWhen should you NOT use microfrontends?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint