• Stars
    star
    421
  • Rank 102,977 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Demonstration of how to gradually upgrade an app to a new version of React

Demo of Gradual React Upgrades

This is a demo of how you can configure a build system to serve two different versions of React side by side in the same app. This is not optimal, and should only be used as a compromise to prevent your app from getting stuck on an old version of React.

Learn more about Gradual Upgrades.

Contributing

This demo is built with Create React App, although it is generic and doesn't assume a particular build system.

Examples With Other Build Systems

If you recreate this demo with a different build system, please send a pull request to link to it.

These examples are maintained by the community:

Why NOT Do This

Note that this approach is meant to be an escape hatch, not the norm.

Normally, we encourage you to use a single version of React across your whole app. When you need to upgrade React, it is better to try to upgrade it all at once. We try to keep breaking changes between versions to the minimum, and often there are automatic scripts ("codemods") that can assist you with migration. You can always find the migration information for any release on our blog.

Using a single version of React removes a lot of complexity. It is also essential to ensure the best experience for your users who don't have to download the code twice. Always prefer using one React if you can.

Why Do This

However, for some apps that have been in production for many years, upgrading all screens at once may be prohibitively difficult. For example, React components written in 2014 may still rely on the unofficial legacy context API (not to be confused with the modern one), and are not always maintained.

Traditionally, this meant that if a legacy API is deprecated, you would be stuck on the old version of React forever. That prevents your whole app from receiving improvements and bugfixes. This repository demonstrates a hybrid approach. It shows how you can use a newer version of React for some parts of your app, while lazy-loading an older version of React for the parts that haven't been migrated yet.

This approach is inherently more complex, and should be used as a last resort when you can't upgrade.

Version Requirements

This demo uses two different versions of React: React 17 for "modern" components (in src/modern), and React 16.8 for "legacy" components (in src/legacy).

We still recommend upgrading your whole app to React 17 in one piece. The React 17 release intentionally has minimal breaking changes so that it's easier to upgrade to. In particular, React 17 solves some problems with nesting related to event propagation that earlier versions of React did not handle well. We expect that this nesting demo may not be as useful today as during a future migration from React 17 to the future major versions where some of the long-deprecated APIs may be removed.

However, if you're already stuck on an old version of React, you may found this approach useful today. If you remove a Hook call from src/shared/Clock.js, you can downgrade the legacy React all the way down to React 16.3. If you then remove Context API usage from src/legacy/createLegacyRoot.js, you can further downgrade the legacy React version, but keep in mind that the usage of third-party libraries included in this demo (React Router and React Redux) may need to be adjusted or removed.

Installation

To run this demo, clone the project, open its folder and execute:

npm install
npm start

If you want to test the production build, you can run instead:

npm install
npm run build
npx serve -s build

This sample app uses client-side routing and consists of two routes:

  • / renders a page which uses a newer version of React. (In the production build, you can verify that only one version of React is being loaded when this route is rendered.)
  • /about renders a page which uses an older version of React for a part of its tree. (In the production build, you can verify that both versions of React are loaded from different chunks.)

The purpose of this demo is to show some nuances of such setup:

  • How to install two versions of React in a single app with npm side by side.
  • How to avoid the "invalid Hook call" error while nesting React trees.
  • How to pass context between different versions of React.
  • How to lazy-load the second React bundle so it's only loaded on the screens that use it.
  • How to do all of this without a special bundler configuration.

How It Works

File structure is extremely important in this demo. It has a direct effect on which code is going to use which version of React. This particular demo is using Create React App without ejecting, so it doesn't rely on any bundler plugins or configuration. The principle of this demo is portable to other setups.

Dependencies

We will use three different package.jsons: one for non-React code at the root, and two in the respective src/legacy and src/modern folders that specify the React dependencies:

  • package.json: The root package.json is a place for build dependencies (such as react-scripts) and any React-agnostic libraries (for example, lodash, immer, or redux). We do not include React or any React-related libraries in this file.
  • src/legacy/package.json: This is where we declare the react and react-dom dependencies for the "legacy" trees. In this demo, we're using React 16.8 (although, as noted above, we could downgrade it further below). This is also where we specify any third-party libraries that use React. For example, we include react-router and react-redux in this example.
  • src/modern/package.json: This is where we declare the react and react-dom dependencies for the "modern" trees. In this demo, we're using React 17. Here, we also specify third-party dependencies that use React and are used from the modern part of our app. This is why we also have react-router and react-redux in this file. (Their versions don't strictly have to match their legacy counterparts, but features that rely on context may require workarounds if they differ.)

The scripts in the root package.json are set up so that when you run npm install in it, it also runs npm intall in both src/legacy and src/modern folders.

Note: This demo is set up to use a few third-party dependencies (React Router and Redux). These are not essential, and you can remove them from the demo. They are included so we can show how to make them work with this approach.

Folders

There are a few key folders in this example:

  • src: Root of the source tree. At this level (or below it, except for the special folders noted below), you can put any logic that's agnostic of React. For example, in this demo we have src/index.js which is the app's entry point, and src/store.js which exports a Redux store. These regular modules only execute once, and are not duplicated between the bundles.
  • src/legacy: This is where all the code using the older version of React should go. This includes React components and Hooks, and general product code that is only used by the legacy trees.
  • src/modern: This is where all the code using the newer version of React should go. This includes React components and Hooks, and general product code that is only used by the modern trees.
  • src/shared: You may have some components or Hooks that you wish to use from both modern and legacy subtrees. The build process is set up so that everything inside src/shared gets copied by a file watcher into both src/legacy/shared and src/modern/shared on every change. This lets you write a component or a Hook once, but reuse it in both places.

Lazy Loading

Loading two Reacts on the same page is bad for the user experience, so you should strive to push this as far as possible from the critical path of your app. For example, if there is a dialog that is less commonly used, or a route that is rarely visited, those are better candidates for staying on an older version of React than parts of your homepage.

To encourage only loading the older React when necessary, this demo includes a helper that works similarly to React.lazy. For example, src/modern/AboutPage.js, simplified, looks like this:

import lazyLegacyRoot from './lazyLegacyRoot';

// Lazy-load a component from the bundle using legacy React.
const Greeting = lazyLegacyRoot(() => import('../legacy/Greeting'));

function AboutPage() {
  return (
    <>
      <h3>This component is rendered by React ({React.version}).</h3>
      <Greeting />
    </>
  );
}

As a result, only if the AboutPage (and as a result, <Greeting />) gets rendered, we will load the bundle containing the legacy React and the legacy Greeting component. Like with React.lazy(), we wrap it in <Suspense> to specify the loading indicator:

<Suspense fallback={<Spinner />}>
  <AboutPage />
</Suspense>

If the legacy component is only rendered conditionally, we won't load the second React until it's shown:

<>
  <button onClick={() => setShowGreeting(true)}>
    Say hi
  </button>
  {showGreeting && (
    <Suspense fallback={<Spinner />}>
      <Greeting />
    </Suspense>
  )}
</>

The implementation of the src/modern/lazyLegacyRoot.js helper is included so you can tweak it and customize it to your needs. Remember to test lazy loading with the production builds because the bundler may not optimize it in development.

Context

If you have nested trees managed by different versions of React, the inner tree won't "see" the outer tree's Context.

This breaks third-party libraries like React Redux or React Router, as well as any of your own usage of Context (for example, for theming).

To solve this problem, we read all the Contexts we care about in the outer tree, pass them to the inner tree, and then wrap the inner tree in the corresponding Providers. You can see this in action in two files:

  • src/modern/lazyLegacyRoot.js: Look for useContext calls, and how their results are combined into a single object that is passed through. You can read more Contexts there if your app requires them.
  • src/legacy/createLegacyRoot.js: Look for the Bridge component which receives that object and wraps its children with the appropriate Context Providers. You can wrap them with more Providers there if your app requires them.

Note that, generally saying, this approach is somewhat fragile, especially because some libraries may not expose their Contexts officially or consider their structure private. You may be able to expose private Contexts by using a tool like patch-package, but remember to keep all the versions pinned because even a patch release of a third-party library may change the behavior.

Nesting Direction

In this demo, we use an older React inside an app managed by the newer React. However, we could rename the folders and apply the same approach in the other direction.

Event Propagation

Note that before React 17, event.stopPropagation() in the inner React tree does not prevent the event propagation to the outer React tree. This may cause unexpected behavior when extracting a UI tree like a dialog to use a separate React. This is because prior to React 17, both Reacts would attach the event listener at the document level. React 17 fixes this by attaching handlers to the roots. We strongly recommend upgrading to React 17 before considering the nesting strategy for future upgrades.

Gotchas

This setup is unusual, so it has a few gotchas.

  • Don't add package.json to the src/shared folder. For example, if you want to use an npm React component inside src/shared, you should add it to both src/modern/package.json and src/legacy/package.json instead. You can use different versions of it but make sure your code works with both of them — and that it works with both Reacts!
  • Don't use React outside of the src/modern, src/legacy, or src/shared. Don't add React-related libraries outside of src/modern/package.json or src/legacy/package.json.
  • Remember that src/shared is where you write shared components, but the files you write there are automatically copied into src/modern/shared and src/legacy/shared, from which you should import them. Both of the target directories are in .gitignore. Importing directly from src/shared will not work because it is ambiguous what react refers to in that folder.
  • Keep in mind that any code in src/shared gets duplicated between the legacy and the modern bundles. Code that should not be duplicated needs to be anywhere else in src (but you can't use React there since the version is ambiguous).
  • You'll want to exclude src/*/node_modules from your linter's configuration, as this demo does in .eslintignorerc.

This setup is complicated, and we don't recommend it for most apps. However, we believe it is important to offer it as an option for apps that would otherwise get left behind. There might be ways to simplify it with a layer of tooling, but this example is intentionally showing the low-level mechanism that other tools may build on.

License

This example is MIT licensed.

More Repositories

1

react.dev

The React documentation website
TypeScript
11,041
star
2

react-transition-group

An easy way to perform animations when a React component enters or leaves the DOM
JavaScript
10,160
star
3

react-router-redux

Ruthlessly simple bindings to keep react-router and redux in sync
JavaScript
7,808
star
4

react-modal

Accessible modal dialog component for React
JavaScript
7,350
star
5

react-rails

Integrate React.js with Rails views and controllers, the asset pipeline, or webpacker.
JavaScript
6,746
star
6

react-router-tutorial

JavaScript
5,523
star
7

rfcs

RFCs for changes to React
5,502
star
8

server-components-demo

Demo app of React Server Components.
JavaScript
4,242
star
9

react-basic

A description of the conceptual model of React without implementation burden.
4,181
star
10

react-codemod

React codemod scripts
JavaScript
4,164
star
11

react-docgen

A CLI and library to extract information from React component files for documentation generation purposes.
TypeScript
3,646
star
12

react-tutorial

Code from the React tutorial.
JavaScript
3,290
star
13

react-tabs

An accessible and easy tab component for ReactJS.
JavaScript
3,089
star
14

react-chartjs

common react charting components using chart.js
JavaScript
2,928
star
15

react-future

Specs & docs for potential future and experimental React APIs and JavaScript syntax.
JavaScript
2,819
star
16

express-react-views

This is an Express view engine which renders React components on server. It renders static markup and *does not* support mounting those views on the client.
JavaScript
2,734
star
17

react-a11y

Identifies accessibility issues in your React.js elements
JavaScript
2,343
star
18

React.NET

.NET library for JSX compilation and server-side rendering of React components
C#
2,293
star
19

react-autocomplete

WAI-ARIA compliant React autocomplete (combobox) component
JavaScript
2,165
star
20

react-art

React Bridge to the ART Drawing Library
JavaScript
1,994
star
21

react-php-v8js

PHP library that renders React components on the server
PHP
1,326
star
22

react-magic

Automatically AJAXify plain HTML with the power of React. It's magic!
JavaScript
947
star
23

zh-hans.react.dev

React documentation website in Simplified Chinese
TypeScript
909
star
24

core-notes

Weekly meeting notes from the React core team
900
star
25

ko.react.dev

React documentation website in Korean
TypeScript
715
star
26

ru.react.dev

React documentation website in Russian / Официальная русская версия сайта React
TypeScript
699
star
27

pt-br.react.dev

🇧🇷 React documentation website in Portuguese (Brazil)
TypeScript
484
star
28

react-lifecycles-compat

Backwards compatibility polyfill for React class components
JavaScript
459
star
29

id.react.dev

(Work in progress) React documentation website in Indonesian
TypeScript
312
star
30

react-timer-mixin

TimerMixin provides timer functions for executing code in the future that are safely cleaned up when the component unmounts
JavaScript
308
star
31

es.react.dev

React documentation website in Spanish / Documentación del sitio web de React en Español
TypeScript
283
star
32

translations.react.dev

Nexus of resources and tools for translating the React docs.
JavaScript
260
star
33

ja.react.dev

React documentation website in Japanese
TypeScript
258
star
34

react-static-container

Renders static content efficiently by allowing React to short-circuit the reconciliation process.
JavaScript
223
star
35

fa.react.dev

(Work in progress) React documentation website in Persian
TypeScript
190
star
36

tr.react.dev

React documentation website in Turkish
TypeScript
165
star
37

ar.react.dev

React documentation website in Arabic 📘⚛️ — وثائق React باللغة العربية
TypeScript
138
star
38

uk.react.dev

🇺🇦 React documentation website in Ukrainian / Офіційна українська версія сайту React
TypeScript
134
star
39

hi.react.dev

(Work in progress) React documentation website in Hindi
TypeScript
115
star
40

zh-hant.react.dev

(Work in progress) React documentation website in Traditional Chinese
TypeScript
106
star
41

bn.react.dev

(Work in progress) React documentation website in Bengali
TypeScript
102
star
42

fr.react.dev

Version française du site de documentation officiel de React
TypeScript
98
star
43

vi.react.dev

(Work in progress) React documentation website in Vietnamese
TypeScript
89
star
44

react-bower

[DISCONTINUED] Bower package for React
JavaScript
69
star
45

legacy.reactjs.org

An archived copy of the legacy React documentation website
JavaScript
62
star
46

bn.reactjs.org

(Work in progress) React documentation website in Bengali
JavaScript
54
star
47

ta.reactjs.org

(Work in progress) React documentation website in Tamil
JavaScript
52
star
48

pl.react.dev

React documentation website in Polish
TypeScript
49
star
49

az.react.dev

🇦🇿 React documentation website in Azerbaijani
TypeScript
43
star
50

rackt-codemod

Codemod scripts for Rackt libraries
JavaScript
40
star
51

th.reactjs.org

(Work in progress) React documentation website in Thai
JavaScript
40
star
52

mn.react.dev

(Work in progress) React documentation website in Mongolian
TypeScript
38
star
53

uz.reactjs.org

(Work in progress) React documentation website in Uzbek
TypeScript
36
star
54

de.react.dev

(Work in progress) React documentation website in German
TypeScript
35
star
55

ml.react.dev

(Work in progress) React documentation website in Malayalam
TypeScript
32
star
56

si.reactjs.org

(Work in progress) React documentation website in Sinhala
JavaScript
32
star
57

it.react.dev

(Work in progress) React documentation website in Italian
TypeScript
32
star
58

be.react.dev

(Work in progress) React documentation website in Belarusian
TypeScript
30
star
59

ur.reactjs.org

(⚠️ Beta Docs Translation only) React documentation website in Urdu. Check details in https://github.com/reactjs/ur.reactjs.org/issues/1#issuecomment-949791355
TypeScript
29
star
60

he.react.dev

(Work in progress) React documentation website in Hebrew
TypeScript
28
star
61

ku.reactjs.org

(Work in progress) React documentation website in Kurdish
JavaScript
28
star
62

ml.reactjs.org

(Work in progress) React documentation website in Malayalam
JavaScript
25
star
63

ur.react.dev

(Work in progress) React documentation website in Urdu
TypeScript
25
star
64

el.reactjs.org

(Work in progress) React documentation website in Greek
JavaScript
25
star
65

hu.react.dev

Hungarian 🇭🇺 React ⚛ documentation 📚 / React magyar dokumentációja
TypeScript
25
star
66

gu.react.dev

(Work in progress) React documentation website in Gujarati
TypeScript
24
star
67

pt-PT.reactjs.org

(Work in progress) React documentation website in Portuguese (Portugal) 🇵🇹
JavaScript
23
star
68

ne.reactjs.org

(Work in progress) React documentation website in Nepali
JavaScript
23
star
69

gu.reactjs.org

(Work in progress) React documentation website in Gujarati
JavaScript
22
star
70

te.reactjs.org

(Work in progress) React documentation website in Telugu
JavaScript
22
star
71

hy.reactjs.org

(Work in progress) React documentation website in Armenian - https://hy.reactjs.org
JavaScript
21
star
72

km.reactjs.org

(Work in progress) React documentation website in Central Khmer
JavaScript
20
star
73

bg.reactjs.org

(Work in progress) React documentation website in Bulgarian
JavaScript
16
star
74

ro.reactjs.org

(Work in progress) React documentation website in Romanian
JavaScript
16
star
75

si.react.dev

(Work in progress) React documentation website in Sinhala
TypeScript
14
star
76

kn.reactjs.org

(Work in progress) React documentation website in Kannada
JavaScript
13
star
77

tl.reactjs.org

(Work in progress) React documentation website in Tagalog
TypeScript
13
star
78

ka.reactjs.org

(Work in progress) React documentation website in Georgian
JavaScript
11
star
79

sv.reactjs.org

(Work in progress) React documentation website in Swedish
JavaScript
9
star
80

te.react.dev

(Work in progress) React documentation website in Telugu
TypeScript
9
star
81

sw.react.dev

(Work in progress) React documentation website in Swahili
TypeScript
9
star
82

nl.reactjs.org

(Work in progress) React documentation website in Dutch
JavaScript
8
star
83

ta.react.dev

(Work in progress) React documentation website in Tamil
TypeScript
8
star
84

ca.reactjs.org

(Work in progress) React documentation website in Catalan
JavaScript
7
star
85

lt.reactjs.org

(Work in progress) React documentation website in Lithuanian
JavaScript
6
star
86

ht.reactjs.org

(Work in progress) React documentation website in Haitian Creole
JavaScript
5
star
87

sr.reactjs.org

(Work in progress) React documentation website in Serbian
TypeScript
5
star
88

is.react.dev

(Work in progress) React documentation website in Icelandic
TypeScript
4
star
89

kk.react.dev

🇰🇿 React documentation website in Kazakh / React сайтының ресми қазақша нұсқасы
TypeScript
4
star
90

cs.react.dev

(Work in progress) React documentation website in Czech
TypeScript
4
star
91

sq.reactjs.org

(Work in progress) React documentation website in Albanian
TypeScript
3
star
92

sr.react.dev

(Work in progress) React documentation website in Serbian
TypeScript
3
star
93

reactjs.github.io

HTML
2
star
94

my.reactjs.org

(Work in progress) React documentation website in Burmese
JavaScript
2
star
95

lo.react.dev

(Work in progress) React documentation website in Lao
TypeScript
2
star
96

fi.react.dev

(Work in progress) React documentation website in Finnish
TypeScript
2
star
97

sw.reactjs.org

(Work in progress) React documentation website in Swahili
JavaScript
2
star
98

mk.react.dev

(Work in progress) React documentation website in Macedonian
TypeScript
1
star
99

tg.reactjs.org

(Work in progress) React documentation website in Tajik
TypeScript
1
star