0Pricing
React Academy · Lesson

Sharing State and Routing Between Remotes

Share a single React Router instance and Zustand store across independently deployed micro-frontends.

Sharing State and Routing Between Remotes is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The State Sharing Challenge

Each Module Federation remote is an independently built and deployed application. Its state (React useState, Context, Zustand stores) lives in its own JavaScript module scope. The host and remotes don't automatically share state — you must architect the sharing explicitly.

Option 1: URL as Shared State

The simplest state sharing mechanism is the URL: any remote can read URL params, query strings, and hash values without any cross-remote communication. This works well for primary navigation state, filter values, and any state that should be bookmarkable and shareable via link.

Option 2: CustomEvents for Loose Coupling

CustomEvents via the browser's EventTarget allow remotes to communicate without direct imports. A remote dispatches window.dispatchEvent(new CustomEvent('cart:updated', { detail: cart })), and any other module (host or other remotes) listens with window.addEventListener('cart:updated', handler). No shared module needed.

Option 3: A Shared State Remote

Create a dedicated "state remote" that exposes a Zustand or Jotai store. Both the host and other remotes import from this state remote and get the same store instance — because it's a shared singleton. This is powerful for complex shared state (auth, cart, user preferences) but adds a deployment dependency.

Option 4: Host Owns State, Passes Callbacks

The shell (host) owns all shared state and passes it down to remotes as props or via Context. Remotes receive callbacks like onCartUpdate and navigate as props. This keeps state centralized in the host but requires the host to know about remote APIs — creating tight coupling.

React Router in Module Federation

React Router's history and location state is provided by a Router component (BrowserRouter or createBrowserRouter). There must be exactly one Router in a Module Federation setup — placing it in each remote would create multiple competing routing contexts, causing navigation conflicts.

Host Owns the Router

The architectural rule is: the host (shell) provides the Router. Remotes receive routing props from the host (or read from the URL directly). A remote should never create its own BrowserRouter — it would conflict with the host's Router and break navigation for the entire application.

Sharing Context Across Remotes

React Context values are tied to a specific React instance and Context object. For a Context to work across host and remote, both must import the same Context object from the same source — typically the shared state remote or a shared utilities package. The React singleton ensures the same Context works across all remotes.

Shell Architecture Pattern

In a clean Module Federation architecture, the shell (host) owns: the HTML shell, the Router provider, the Auth context, the theme/design token provider, and global navigation. Remotes own their feature domain content. The shell composes remotes into the layout without knowing their implementation details.

Avoiding Tight Coupling

The biggest Module Federation antipattern is tight host-remote coupling: the host imports types from the remote, the remote imports components from the host, or they share a common npm package that both must update simultaneously. These couplings defeat the purpose of independent deployment.

Communication Contracts

Define explicit contracts between host and remotes: the props a remote component accepts, the CustomEvents it dispatches, and the URL params it reads. Document these contracts as TypeScript interfaces in a shared types package. This allows teams to evolve independently within the agreed contract.

React Router Placement in Module Federation

Where should the React Router provider (BrowserRouter) be placed in a Module Federation architecture?

Lesson Recap

State sharing across Module Federation remotes can use URL state (simplest), CustomEvents (loosely coupled), a shared state remote (centralized store), or host-owned state passed as props. The host must own the single Router provider — remotes should never create their own. Clear communication contracts via TypeScript interfaces keep teams decoupled and enable true independent deployment.

Frequently asked questions

Is the “Sharing State and Routing Between Remotes” lesson free?

Yes — the full text of “Sharing State and Routing Between Remotes” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sharing State and Routing Between Remotes”?

Share a single React Router instance and Zustand store across independently deployed micro-frontends. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sharing State and Routing Between Remotes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Module Federation: Dynamic Runtime Imports
  2. Configuring Host and Remote React Apps
  3. Sharing State and Routing Between Remotes
  4. Versioning, Deployment, and Orchestration
← Back to React Academy