In the documentation, the preferred way React
to build a new application is (create-react-app).React
CRA
CRA
It was launched in 2016. At that time, there were no systematic React
scaffolding tools for everyone to use. In addition, it was an official tool and it was very popular as soon as it was launched. As of now, CRA
the warehouse has harvested nearly 100,000 yuan star
.
However, over time, many excellent alternatives have emerged, such as parcel
the vite
provided React
templates.
However, CRA
its progress is slowing down, and its last submission dates back to September 8 last year:
In addition, CRA
the support for some popular tools is not very good. For example, TailwindCSS
it is not recommended in the documentation CRA
:
Recently, Theo , a front-end Internet celebrity with 100,000 YouTube fans, React
launched a PR in the document warehouse , calling on React
documents to no longer be recommended by default CRA
, but should be used Vite
as the first choice for building applications.
Judging by the number of onlookers, you can see how concerned everyone is about this sensitive issue:
So, React
how does the team view this issue? Will they Vite
make it their first choice when building apps?
This article will talk about Dan ( React
core member)’s views on this issue.
Welcome to join the human high-quality front-end exchange group and lead flying
Contents
CRA’s positioning
Since it is the target of public criticism CRA
, first we need to understand its positioning CRA
under React
the system, and then see Vite
if we can replace the former under this positioning.
CRA
The period of its birth (2016) was SPA
the hottest period (single-page applications). At that time, he solved two pain points very well:
0Configuration initialization project
This does not need to be introduced too much. A CSR
(client-side rendering) React
project can be generated by executing the following command:
npx create-react-app project name
Integrated tool chain
CRA
Encapsulate some of the engineering best practices at the time under react-scripts
the package and smooth out the incompatibilities of these tools.
Developers enjoy out-of-the-box best practices and don’t have to worry about the impact of certain tool upgrades on the project ( CRA
it will be dealt with).
Later, many excellent scaffolding tools (such as Vite
, Parcel
) also followed this out-of-the-box concept.
In addition to the above two points, with CRA
the popularity of , React
the team will also use it as a rapid distribution channel for new features, such as:
Fast Refresh
(ForReact
hot updates, component status will not be lost)Hooks
lint
A series of rules after the launch
Relying on CRA
the huge installed capacity and usage, these integrated CRA
features can be quickly deployed into developers’ projects to quickly increase penetration.
Just imagine, without CRA
the promotion, it would be difficult for Hooks
the lint
rules to have such a high popularity among developers, and Hooks
the concept would not sweep the entire front-end framework field so quickly.
From the above three points, Vite
it can become CRA
a substitute with better performance.
But React
the team’s considerations go beyond that.
Disadvantages of scaffolding tools
Although CRA
it can be used out of the box, the capabilities it provides are not comprehensive. For example, it does not provide:
- State management solution
- Routing plan
- Data request plan
Why not provide it? Because during CRA
the development period, these solutions have not yet formed best practices.
Over time, developers gradually figured out best practices for solving these problems. For example, in the request waterfall problem, consider the following components:
function App () { const [data, update] = useState ( null ); useEffect ( () => { fetch ( 'http://...' ). then ( res => update (res. json ())) }, []) return < Child data = {data}/ > }
Data can only App
be requested after the component is rendered. This request timing is relatively lagging. If Child
you rely data
on requesting your own data, the request will also be lagging due App
to the lagging of the request . This is the request waterfall problem.Child
A common solution to this problem is to converge the logic of requesting data into the routing scheme.
For another example, as the business continues to iterate, the size of the business code is getting larger and larger, and a common optimization method is lazy loading of components.
However, manually performing lazy loading often creates unexpected problems. For example, there is a chart component on the page <Chart/>
. If the developer loads this component lazily, but the component on mount
requests data at the same time, it will fall into the request waterfall problem.
To completely solve this problem, three types of technical solutions are needed:
- Data request solution (solve data flow problem)
- Routing scheme (solve the problem of data request timing)
- Packaging solution (solve the implementation problem of lazy loading)
There are many similar problems, such as CSR
the slow rendering speed of the first screen (which needs to SSR
be solved).
It can be seen that CRA
it only provides CSR
an out-of-the-box template for the environment, but as the project becomes more and more complex, some business details CRA
do not provide out-of-the-box solutions.
From this perspective, Vite
you will still face the same problem even if you switch to .
A new era framework
As the best practices for various common problems are explored, some frameworks based on React
them , such as Next.js
.Remix
Among them, Remix
it is React-Router
a full-stack framework that is gradually developed based on (routing solution) and includes routing, data request, and rendering.
So, can it be CRA
iterated into a full-stack framework like this to solve the lack of various best practices once and for Next.js
all ?Remix
CRA
React
The team believes that doing so requires extremely high development costs, and as the times evolve, there will always be more CRA
unsupported best practices (just like the problem he is currently facing), which CRA
will one day be eliminated again.
Therefore, this solution is not advisable.
Since this solution is undesirable, the Vite
replacement CRA
solution is also undesirable. Because simple use Vite
does not solve the lack of best practices, and those best practices must be implemented on this basis (such as routing, data requests…), then it is back to developing a full-stack framework .
In the end, React
the team prefers the following solution: it will be used CRA
as a scaffolding tool. After startup, it will recommend different frameworks according to the user’s different scenario needs (such as yes SSR
or no ), and then it will be used as a fallback solution when the framework is not used .CSR
CRA
Moreover, in terms of implementation, it is possible to webpack
switch to Vite
.
Summarize
From React
the team’s thinking, we can find that React
it has always positioned itself as a state-driven UI library.
With the development of the times, using this library alone can no longer meet daily development needs. Frameworks that use React + to implement various best practice patterns based on the bottom layer will become more and more popular.
Recently, Next.js
it reached the 100,000 star
achievement and became the 14th ranked warehouse Github
in China , indirectly confirming this trend.star
Back to the opening question: React
why not Vite
recommend it as the default?
If the packaging tool is used Vite
instead webpack
of CRA
AS, it may be possible in the future. However, this is not the primary issue.