Framework - Interview Question 7 - What are the advantages and disadvantages of hooks in React

e61362e229eae51b724f6f7046e24517.png

In reactis hooksa very abstract concept, and it is often not very friendly to beginners. Compare the advantages and disadvantages Reactofhooks

advantage

[1]. The code is highly readable. hooksBefore use, publish/subscribe custom events need to be mounted on componentDidMountthe life cycle, and then need to componentWillUnmountbe cleared in the life cycle. After use hooks, pass useEffect, you can put componentDidMountthe life cycle, componentDidUpdatelife cycle, There is also componentWillUnmounta centralized life cycle to facilitate code maintenance

[2]. The level of the component is shallower. Before using it hooks, the method of high-order components is usually used Hocto realize the common state of multiple components and enhance the function of the component. This increases the cost of component rendering and affects performance. However, Hooksin You can use the custom hookscomponent useXXx()method to put the shared state between multiple components into the custom hooks, and you can easily share the state

[3]. No need to think about the pointing problem of classclass components , and there is no need to use it to obtain data and methods in componentsthishooksthis.state

[4]. State logic can be extracted from components, so that these logics can be tested and reused separately, Hookallowing you to reuse state logic without modifying the component structure, which makes sharing between components Hookmore convenient, that is Can greatly reduce redundant code, especially for those scenarios that need to reuse logic

[5]. No destructive changes, Hookwill not affect Reactthe understanding of the concept, provides a more direct , , , , and life cycle Hookfor known concepts, and provides a powerful way to combine themReactAPIpropsstatecontextrefsHook

[6]. Easier to test: Since hooksthey are pure JavaScriptfunctions, they are easy to write unit tests and simulate

shortcoming

[1]. useEffectYou can't write too many things in one. Divide each different function into multiple modules for use, and divide each function block into multiple modules. It follows the single useEffect-responsibility model in software design. Including , and these three life cycles, the life cycle of other class components is not supportedhooksuseEffectconponentDidMountcomponentDidUpdatecomponentWillUnmountclass

[2]. Don't classcall it in the component hook, it is invalid, it can't fully simulate the life cycle of the class component, although it can be used useEffect hookto simulate, but it needs more thinking and planning to use

[3]. HooksIt is a new feature, and there are some compatibility issues. Compared with the class component method, the learning curve is relatively steep, and it takes some time to adapt to this programming mode.

ReactThe main hooksadvantages are to improve the readability and performance of the code, facilitate the maintenance and iteration of the code, and at the same time better achieve state sharing, but it is also necessary to choose the most appropriate method in combination with specific scenarios and requirements during use , nothing more than having hook, there is one more technical solution option

JS interview question 18-Talk about the performance indicators of performance optimization in web pages and how to quantify them

2023-03-14

81579f09edb2b9d83291858657ad04b3.jpeg

JS Interview Question 17 - Compare server-side rendering with client-side rendering

2023-03-12

147bb2a958085814c5253d6b99f0e2d6.jpeg

In what scenario is the event extension in JS interview question 16-Es6 used?

2023-03-11

13573ced48ae0eb53fde671e96469064.jpeg

In what scenario is the event extension in JS interview question 16-Es6 used?

2023-03-10

f637c3cd9fe13388a5718a811d034729.jpeg

Framework - interview question 6 - talk about the hook function of Vue2 and Vue3

2023-03-09

ed052255bce12b84752e8e34fa47c1b9.jpeg

One article to understand the operation in the Internet

2023-03-03

561402577144638bb9f992802841a610.jpeg

What you need to know from changing your resume to interviewing

2023-03-02

5c153e02cae11bdaf783182cd8c5b028.jpeg

Before joining the job-questions that job seekers must understand in advance

2023-03-01

3ef840ce62d88899168b2bcf83378c70.jpeg

You always copy the code, right? ChatGPT teaches you how to write code

2023-03-17

1cbf3b3325480a7bce4bdba265139f5f.jpeg

36 years old this year, graduated from Beiyou with a master's degree, worked in Byte, Ali, was recently laid off, and can only go to outsourcing

2023-03-17

ce5fb1fec04f177b3338c8d899d28edb.jpeg

8ca955909237fa1c61b0cfb205a4128a.jpeg

841317d1e473e1780d75844144b911b2.png

Click on the lower left corner to see more

ecd231ea2316cacab231d7c3fbae0fc4.gif

Guess you like

Origin blog.csdn.net/wzc_coder/article/details/129645730