Why React doesn't use Vite as the first choice for building apps
Why doesn’t React use Vite as the first choice for building applications? The following article will talk to you about the reasons why React does not recommend Vite as the default recommendation. I hope it will be helpful to everyone!
In the React
documentation, the preferred way to build a new React
application is CRA
(create-react-app).
CRA
was launched in 2016. At that time, there was no systematic React
scaffolding tool for everyone to use. In addition, this is an official tool, and it was well received as soon as it was launched. Welcome. As of now, the CRA
warehouse has harvested nearly 10wstar
.
However, as time goes by, many excellent alternatives have emerged, such as the React
templates provided by parcel
and vite
.
The progress of CRA
itself is slowing down, and its last submission dates back to September 8 last year:
In addition, CRA
's support for some popular tools is not very good. For example, CRA
is not recommended in the TailwindCSS
document:
Recently, front-end Internet celebrity Theo with 100,000 fans on YouTube launched a PR
in the React document warehouse, calling for ## The #React documentation should no longer recommend
CRA by default, but should instead use
Vite as the first choice for building applications. [Related recommendations:
Redis Video Tutorial, Programming Video]
React team view this issue? Will they make
Vite their first choice when building apps?
Dan (React core member)’s views on this issue.
Human high-quality front-end exchange group, lead the
CRA's positioningSince the public target isCRA, then First we need to understand the positioning of
CRA under the
React system, and then let’s see if
Vite can replace the former under this positioning. The period when
CRA was born (2016) was the hottest period for
SPA (single page application). At that time, he solved two pain points very well:
CSR(Client-side rendering)'s
React project:
npx create-react-app 项目名复制代码Integrated tool chain
CRA encapsulates some of the engineering best practices at the time Under the
react-scripts package, and smooth out the incompatibilities of these tools.
CRA will handle it).
Vite,
Parcel) also followed this
out-of-the-box concept. In addition to the above two points, with the popularity of
, the React
team will also use it as a rapid distribution channel for new features, such as:
- Fast Refresh
(Hot update for
React
without losing component state) - Hooks
A series of
lint
rules after the launch Relying on the huge installed capacity and usage of
, these are integrated into CRA
features can be quickly deployed to developers' projects to quickly increase popularity. Just imagine, without the promotion of
, it would be difficult for the lint
rules of Hooks
to have such a high popularity among developers, ## The concept of #Hooks will not sweep across the entire front-end framework field so quickly.
From the above three points,
Vite
CRA.
However, the
React
Disadvantages of scaffolding tools
CRA
is ready to use out of the box, the capabilities it provides are not comprehensive. For example, it does not provide:状态管理方案
路由方案
数据请求方案
为什么不提供呢?因为在CRA
发展的时期,这些方案还未形成最佳实践。
随着时间发展,开发者逐渐摸索出解决这些问题的最佳实践。比如请求瀑布问题,考虑如下组件:
function App() { const [data, update] = useState(null); useEffect(() => { fetch('http://...').then(res => update(res.json())) }, []) return <child></child>}复制代码
只有当App
组件渲染后才能开始请求数据,这个请求时机是比较滞后的,如果Child
依赖data
来请求自己的数据,那么由于App
请求的滞后导致Child
的请求也滞后了,这就是请求瀑布问题。
这个问题常见的解决方法是 —— 将请求数据的逻辑收敛到路由方案中。
再比如,随着业务不断迭代,业务代码体积越来越大,常见的优化手段是懒加载组件。
但是,手动执行懒加载常常会产生意料之外的问题。比如,页面中有个图表组件<chart></chart>
,如果开发者懒加载了这个组件,但是该组件在on mount
时请求数据,这又会陷入请求瀑布问题。
要彻底解决这个问题,需要配合3类技术方案:
数据请求方案(解决数据流向问题)
路由方案(解决数据请求时机问题)
打包方案(解决懒加载的实现问题)
类似的问题还有很多,比如CSR
首屏渲染速度慢的问题(需要通过SSR
解决)。
可见,CRA
仅仅提供了CSR
环境下一个开箱即用的模版,但是随着项目变得越来越复杂,一些业务细节问题CRA
是没有提供开箱即用的解决方案的。
从这个角度看,即使切换到Vite
还是会面临同样的问题。
新时代的框架
随着各种常见问题的最佳实践被探索出来,逐渐诞生了一些以React
为基础,集成各种业务问题最佳实践的框架,比如Next.js
、Remix
。
其中,Remix
就是以React-Router
(路由解决方案)为基础,逐渐发展出来的囊括路由、数据请求、渲染为一体的全栈框架。
那么,能否将CRA
迭代为类似Next.js
、Remix
这样的全栈框架,一劳永逸解决CRA
对各种最佳实践的缺失呢?
React
团队认为,这样做需要极高的开发成本,而且随着时代发展,总会出现更多CRA
不支持的最佳实践(就像他当前面临的问题一样),那么CRA
终有一天会被再度淘汰。
所以,这个方案不可取。
既然这个方案不可取,那么用Vite
取代CRA
的方案也不可取。因为单纯使用Vite
并没有解决最佳实践的缺失,必须在此基础上实现那些最佳实践(比如路由、数据请求...),那又回到了开发一个全栈框架。
最终,React
团队更倾向如下解决方案:将CRA
作为一个脚手架工具,启动后会根据用户的不同场景需要(比如是SSR
还是CSR
)推荐不同的框架,再将CRA
作为不使用框架情况下的兜底方案。
并且,在实现上,可能将兜底方案中的webpack
切换为Vite
。
总结
从React
团队的思考可以发现,React
始终将自己定位为一个状态驱动UI的库。
随着时代的发展,单独使用这个库已经不能满足日常开发需要,基于底层使用React + 实现各种最佳实践模式的框架会越来越流行。
最近,Next.js
达到了10wstar
成就,成为Github
中star
排名第14的仓库,间接印证了这种趋势。
回到开篇的问题:React
为什么不将Vite
作为默认推荐?
如果是用Vite
取代webpack
作为CRA
的打包工具,未来可能会。但是,这不是最首要的问题。
如何协助上层的框架更好的服务开发者,才是React
团队首要考虑的问题。
React
不死,他只会逐渐移居幕后。
【推荐学习:javascript视频教程】
The above is the detailed content of Why React doesn't use Vite as the first choice for building apps. For more information, please follow other related articles on the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
