We know that after React
its release Hooks
, it brought a wave of Hooks
enthusiasm in the industry. Many frameworks (such as Vue Composition API
, Solid.js
) have borrowed Hooks
patterns from .
However, even if these frameworks are borrowed from each other Hooks
, the development directions are gradually different due to the different ideas of the framework authors.
For example, in Vue Composition API
, the React useEffect API
benchmark is that watchEffect
in Vue
the document, there is a short paragraph introducing its usage:
In React beta
the document, useEffect
it is introduced in 6 sections:
Why is there such a difference? Let’s start by useEffect
taking a look React
at Vue
the differences in design concepts.
Welcome to join the human high-quality front-end framework group and lead the team
Differences between Vue and React
When Hooks
it first came out, it was seen as an alternative to class components. When introduced in the document Hooks
, it is also compared with class components.
The useEffect
execution timing includes the following three life cycle functions:
componentDidMount
componentDidUpdate
componentWillUnmount
On the other Hooks
hand Vue Composition API
, it also provides watchEffect API
life cycle functions for different scenarios.
The difference in design concepts between the two is already reflected here:
React
As a framework created Facebook
to explore best practices in UI development , the consistent approach is to remain API
stable (for example, it has existed this.setState
since its inception).React
Instead, Vue
it draws on best practices from various frameworks (such as 虚拟DOM
… 响应式更新
).
Therefore, in terms of ease of use, Vue Composition API
it is definitely better React Hooks
, such as:
Hooks
cannot be declared in a conditional statementHooks
Dependencies must be explicitly specified
Moreover, this difference in ease of use will become more and more obvious as the framework iterates.
useEffect will become more and more complex
In line with the principle of maintaining API stability , it is currently useEffect
mainly related to the above three life cycle functions.
However, there will be more triggering opportunities and useEffect
hooks in the future.
Therefore, React
the team is trying to do one thing – downplay useEffect
the relationship with the life cycle, and even downplay useEffect
the relationship with the components (because when talking about components, it is natural to think of the component life cycle).
How to dilute it? The answer is – in strict mode, DEV
the environment will trigger multiple useEffect
callbacks.
If you use useEffect
it componentDidMount/WillUnmount
, this feature is likely to make your code break bug
.
React
The reason why the team does this is to educate developers – useEffect
it has nothing to do with the life cycle. Developers should useEffect
think of this as a synchronization process against a data source .
For example, the following chat room component useEffect
can be regarded as the synchronization process for the chat room connection :
const serverUrl = 'https://localhost:1234' ; function ChatRoom ( { roomId } ) { useEffect ( () => { const connection = createConnection (serverUrl, roomId); connection.connect ( ); return () => { connection.disconnect ( ); }; }, [roomId]); // ... }
When the chat room component mount
, update
, unmount
the corresponding synchronization process should be carried out.
When roomId
changes occur, the corresponding synchronization process should be performed.
In the same way, if React
it is natively supported , the synchronization process should be carried out when the chat room component changes from visible to invisible , and from invisible to visible .Vue
KeepAlive
So, when we look at it from the perspective of when the synchronization process should take placeuseEffect
, the above useEffect
trigger timings are all reasonable.
However, if you look at it from the perspective of life cycle functions , when the features are implemented in useEffect
the future (maybe a certain version of v18) and the components change from visible to invisible , they will be executed in sequence, which will cause People have big heads.Offscreen Component
Vue
KeepAlive
useEffect销毁函数
useEffect回调函数
This is why, as I said above, React
the team has been downplaying useEffect
the relationship with the life cycle, and even downplaying useEffect
the relationship with components.
Everything is to lay a theoretical foundation for linking other features with useEffect in the future . These features don’t make sense from the perspective of components or lifecycle functions .
useEffect
This is why there are 6 relevant sections in the new document .
As a comparison, Vue
what do you do when you encounter a new scene? Apparently new by design API
.
Summarize
Is it better to provide one API
that can cover more scenarios (the document has 6 sections to introduce it), or is it better to provide one for every scenario API
?
Different developers have their own answers.
But one thing is clear, for front-end novices, React
it will become more and more difficult to get started, while Vue
the difficulty of getting started will be kept as smooth as possible.
The front-end novices here may be new people who want to get into the front-end industry, or they may be back-end people who feel that I can also do the front-end .
So, is this a good thing or a bad thing for current practitioners?