Full answer
A big-bang rewrite freezes product work, loses hidden behaviour and often fails. Incremental migration keeps shipping.
Steps:
- Understand and protect. Inventory the codebase (class components, deprecated lifecycles, legacy context, string refs,
defaultPropson functions, CRA config). Add characterization tests and end-to-end tests for the most critical flows, so you can detect regressions. Set up error monitoring and performance baselines. - Upgrade the platform. Move to a supported React version step by step, reading the upgrade guides and running official codemods. Fix warnings in Strict Mode. Replace CRA (deprecated) with Vite or a framework, which can be done separately from React changes. Add TypeScript with
allowJsand migrate file by file. - Establish the target and stop the bleeding. Document the new patterns (function components, hooks, a data-fetching library instead of hand-written Redux thunks, the router choice). Add ESLint rules and code review checks so new code does not add legacy patterns.
- Strangle gradually. Migrate leaf components first, since they have few dependents, and then routes. Classes and function components interoperate, so there is no need to convert everything. For pages built with another framework (jQuery, Angular, old React versions), mount the new React pieces as widgets inside the old shell with their own
createRoot, or put routing at a proxy or micro-frontend boundary, and move pages one at a time. Share design tokens, authentication and analytics between the old and new parts. - Replace state management where it hurts. Redux can stay for global client state while server state moves to TanStack Query or RTK Query. Remove boilerplate when files are touched.
- Measure and finish. Track the percentage of migrated routes, bundle size, errors, and web vitals. Delete the old code and dependencies once nothing uses them.
Risks: running two stacks increases bundle size and inconsistency, so set a time box and an owner. Feature parity checks and gradual rollout (feature flags, percentage traffic) reduce risk. Communicate with the team and product so the migration has support.
The example shows the widget-mounting technique: a React 19 island created inside an existing page container managed by legacy code.
import { createRoot } from 'react-dom/client';
import { act, useState } from 'react';
// Lets act() flush updates in this demo script. Browsers do not need this.
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
function CourseWidget({ title }) {
const [saved, setSaved] = useState(false);
return (
<button onClick={() => setSaved(true)}>
{saved ? 'Saved: ' : 'Save: '}{title}
</button>
);
}
// The "legacy" page: plain DOM that existing code owns.
document.body.innerHTML = '<div id="legacy-shell"><h2>Legacy page</h2><div id="react-widget"></div></div>';
document.querySelector('h2').dataset.owner = 'legacy';
const root = createRoot(document.querySelector('#react-widget'));
await act(async () => { root.render(<CourseWidget title="React basics" />); });
console.log('widget mounted: ' + document.querySelector('#react-widget').textContent);
await act(async () => { document.querySelector('#react-widget button').dispatchEvent(new window.MouseEvent('click', { bubbles: true })); });
console.log('after click: ' + document.querySelector('#react-widget').textContent);
console.log('legacy markup untouched: ' + document.querySelector('h2').textContent + ' (' + document.querySelector('h2').dataset.owner + ')');
await act(async () => { root.unmount(); });
console.log('after unmount, widget container is empty: ' + (document.querySelector('#react-widget').innerHTML === ''));This ran as a script in jsdom. The widget has its own root, so the legacy page can mount and unmount it without a shared React tree. Output of the run: widget mounted: Save: React basics after click: Saved: React basics legacy markup untouched: Legacy page (legacy) after unmount, widget container is empty: true
Key points
- Avoid big-bang rewrites. Protect critical flows with tests, then migrate in steps.
- Upgrade the platform (React, build tool, TypeScript) separately from the architecture changes.
- Use the strangler approach: new routes or widgets in the new stack, mounted in the old shell.
- Add lint rules to stop new legacy code, and use codemods for mechanical changes.
- Track progress with metrics, time-box the coexistence, and delete old code.
Common mistakes
- Starting a full rewrite and freezing feature work.
- Changing React version, build tool and architecture in one release.
- Migrating pages without tests around critical flows.
- Running two stacks forever without a time box.
- Not preventing new legacy code with lint rules.
Follow-ups
- How would you route between the legacy and new apps during the migration?
- How do you decide the order of components to migrate?
- Which codemods would you run first?
Tips
- Lead with why not a rewrite, then give the sequence.
- Separate platform upgrades from architecture changes.
- Mention strangler pattern, tests and metrics.