# Nested React Demo
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.
## You Probably Don't Need 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](https://reactjs.org/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.
## What Is This For?
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](https://reactjs.org/docs/legacy-context.html) (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, open its folder in Terminal and execute:
```sh
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](https://github.com/facebook/react/issues/13991) 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.json`s: 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 exam
没有合适的资源?快使用搜索试试~ 我知道了~
react-17.0.2.zip
共1879个文件
js:1413个
css:80个
json:79个
需积分: 0 0 下载量 96 浏览量
2024-05-04
14:37:31
上传
评论
收藏 5.91MB ZIP 举报
温馨提示
react.js for windows 各个版本,免费下载 前端三大框架之一 react 各个版本,免费下载 如果下载不了,关注我,评论区联系我 React 是一个库。它允许你将组件放在一起,但不关注路由和数据获取。要使用 React 构建整个应用程序,建议使用像 Next.js 或 Remix 这样的全栈 React 框架。
资源推荐
资源详情
资源评论
收起资源包目录
react-17.0.2.zip (1879个子文件)
AUTHORS 42KB
authors 284B
ReactCoffeeScriptClass-test.coffee 16KB
hardware-counter.cpp 12KB
jsc-perf.cpp 5KB
thread-local.cpp 2KB
perf-counters.cpp 1KB
root.css 8KB
style.css 4KB
SettingsShared.css 2KB
codemirror-paraiso-dark.css 2KB
OwnersStack.css 2KB
Profiler.css 2KB
index.css 2KB
SchedulingProfiler.css 2KB
TabBar.css 2KB
InspectedElementView.css 2KB
Element.css 2KB
InspectedElement.css 1KB
hydration.css 1KB
EventTooltip.css 1KB
Components.css 1KB
DevTools.css 1KB
Tree.css 1KB
ErrorBoundary.css 1011B
SidebarCommitInfo.css 1004B
SidebarSelectedFiberInfo.css 991B
StyleEditor.css 986B
InspectedElementHooksTree.css 965B
style.css 918B
Toggle.css 907B
InteractionListItem.css 895B
EditableValue.css 870B
InspectedElementSharedStyles.css 851B
LayoutViewer.css 826B
SidebarInteractions.css 825B
RecordToggle.css 816B
SettingsModal.css 811B
ModalDialog.css 765B
Button.css 697B
SearchInput.css 693B
KeyValue.css 676B
ChartNode.css 626B
HoveredFiberInfo.css 598B
ContextMenuItem.css 592B
ProfilingImportExportButtons.css 522B
Tooltip.css 494B
SnapshotSelector.css 494B
Badge.css 475B
styles.css 460B
SnapshotCommitList.css 397B
Chrome.css 394B
ContextMenuItem.css 388B
HocBadges.css 387B
WhatChanged.css 356B
index.css 348B
App.css 348B
SnapshotCommitListItem.css 338B
NewArrayValue.css 327B
Tooltip.css 326B
UnsupportedVersionDialog.css 314B
styles.css 303B
AutoSizeInput.css 288B
NoCommitData.css 282B
SelectedTreeHighlight.css 269B
ListItem.css 260B
Page.css 243B
NoInteractions.css 240B
ContextMenu.css 227B
ImportButton.css 226B
index.css 189B
List.css 189B
EditableName.css 187B
NewKeyValue.css 159B
ContextMenu.css 158B
CommitFlamegraph.css 151B
ExpandCollapseToggle.css 135B
WarnIfLegacyBackendDetected.css 127B
ReactLogo.css 120B
CanvasPage.css 104B
Interactions.css 101B
index.css 90B
index.css 87B
ButtonIcon.css 69B
Icon.css 63B
CommitRanked.css 60B
RootSelector.css 23B
.editorconfig 294B
.env 45B
.env 30B
.env 26B
.env 25B
.eslintignore 561B
.eslintignore 19B
index.esm.js.flow 34B
index.cjs.js.flow 34B
flowconfig 1KB
grayarrow.gif 111B
.gitattributes 12B
.gitignore 891B
共 1879 条
- 1
- 2
- 3
- 4
- 5
- 6
- 19
资源评论
段子手-168
- 粉丝: 1830
- 资源: 2266
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功