search
HomeWeb Front-endJS TutorialSome common interview questions about React (share)
Some common interview questions about React (share)Aug 04, 2020 pm 05:16 PM
reactInterview questions

Some common interview questions about React (share)

[Related topic recommendations: react interview questions (2020)]

1. redux middleware

Answer: Middle The software provides a third-party plug-in mode to customize the process of intercepting action-> reducer. Become action -> middlewares -> reducer . This mechanism allows us to change the data flow and implement functions such as asynchronous action, action filtering, log output, exception reporting, etc.

Common middleware: redux-logger: provides log output; redux-thunk: handles asynchronous operations; redux-promise: handles asynchronous operations; the return value of actionCreator is promise

2, redux What are the disadvantages

Answer: 1. The data required by a component must be passed from the parent component, and cannot be obtained directly from the store like flux.

2. When the relevant data of a component is updated, even if the parent component does not need to use this component, the parent component will still be re-rendered, which may affect efficiency, or require writing complex shouldComponentUpdate for judgment.

3. How are react components divided into business components and technical components?

Answer: Components are usually divided into UI components and container components according to their responsibilities. UI components are responsible for UI presentation, and container components are responsible for managing data and logic. The two are connected through the connect method provided by React-Redux.

4. React life cycle function

Answer: 1. Initialization phase:

getDefaultProps: Get the default properties of the instance

getInitialState: Get each instance Initialization state

componentWillMount: The component is about to be loaded and rendered on the page

render: The component generates a virtual DOM node here

componentDidMount: The component is actually loaded after it is loaded

2. Running state:

componentWillReceiveProps: Called when the component is about to receive properties

shouldComponentUpdate: When the component receives new properties or new status (can return false , does not update after receiving data, prevents render from being called, and subsequent functions will not continue to be executed)

componentWillUpdate: The component is about to be updated and cannot modify properties and status

render: Component re-rendering

componentDidUpdate: The component has been updated

3. Destruction phase:

componentWillUnmount: The component is about to be destroyed

5. Which periodic function is react performance optimization?

Answer: shouldComponentUpdate This method is used to determine whether the render method needs to be called to redraw the dom. Because the rendering of DOM consumes a lot of performance, if we can write a more optimized DOM diff algorithm in the shouldComponentUpdate method, the performance can be greatly improved.

6. Why does virtual dom improve performance?

Answer: Virtual dom is equivalent to adding a cache between js and real dom, using the dom diff algorithm to avoid unnecessary dom operations. thereby improving performance.

The specific implementation steps are as follows:

1. Use JavaScript object structure to represent the structure of the DOM tree; then use this tree to build a real DOM tree and insert it into the document;

2. When the state changes, reconstruct a new object tree. Then compare the new tree with the old tree, and record the differences between the two trees;

Apply the differences recorded in step 2 to the real DOM tree built in step 1, and the view will be updated.

7. Diff algorithm?

Answer: 1. Decompose the tree structure according to levels and only compare elements at the same level.

2. Add a unique key attribute to each unit of the list structure to facilitate comparison.

3. React will only match components of the same class (the class here refers to the name of the component)

4. In the merge operation, when calling the setState method of the component, React will Marked as dirty. At the end of each event loop, React checks all components marked as dirty and redraws them.

6. Selective subtree rendering. Developers can override shouldComponentUpdate to improve diff performance.

8. React performance optimization solution

Answer: 1) Rewrite shouldComponentUpdate to avoid unnecessary DOM operations.

2) Use the production version of react.js.

3) Use key to help React identify the smallest changes of all subcomponents in the list

9. Briefly describe the idea of ​​flux

Answer: The biggest feature of Flux is the "one-way" of data flow".

1. The user accesses the View

2. The View issues the user's Action

3.The Dispatcher receives the Action and requires the Store to update accordingly

4 After the .Store is updated, a "change" event is issued

5. After the View receives the "change" event, the page is updated

10. What scaffolding was used in the React project? Mern? Yeoman?

Answer: Mern: MERN is a scaffolding tool that can easily generate isomorphic JS applications using Mongo, Express, React and NodeJS. It minimizes installation time and gets you using proven technology to speed development.

11. Do you know React?

Answer: Yes, React is a lightweight component library developed by Facebook. It is used to solve some problems of the front-end view layer, which is the problem of the V layer in MVC. Its internal Instagram website uses React. Built.

12. What problems does React solve?

Answer: Three problems have been solved: 1. Component reuse problem, 2. Performance problem, 3. Compatibility problem:

13. React protocol?

Answer: The agreement that React follows is the "BSD License Patented Open Source Agreement". This agreement is quite strange. If your product does not compete with Facebook, you can use react freely, but if there is competition, Your react license will be revoked

14. Do you understand shouldComponentUpdate?

Answer: React virtual dom technology requires constant diff comparison between dom and virtual dom. If the dom tree is large, this comparison operation will be more time-consuming. Therefore, React provides a patch function such as shouldComponentUpdate. If For some changes, if we do not want a component to be refreshed, or if it is refreshed and remains the same as before, we can use this function to tell React directly, eliminating the need for diff operations and further improving efficiency.

15. How does React work?

Answer: React will create a virtual DOM (virtual DOM). When the state in a component changes, React will first mark the changes in the virtual DOM through the "diffing" algorithm. The second step is reconciliation, and the DOM will be updated with the results of the diff.

16. What are the advantages of using React?

Answer: 1. It is easy to know how a component is rendered by just looking at the render function

2. The introduction of JSX makes the component code more readable and easier Understand the layout of components, or how components refer to each other

3. Support server-side rendering, which can improve SEO and performance

4. Easy to test

5. React only focuses on the View layer, so it can be used with any other framework (such as Backbone.js, Angular.js)

17. There is a difference between the presentation component (Presentational component) and the container component (Container component) What's the difference?

Answer: 1. Display components care about what the components look like. Display specifically accepts data and callbacks through props, and almost never has its own state, but when the display component has its own state, it usually only cares about the UI state rather than the state of the data.

2. Container components are more concerned about how the components operate. Container components provide data and behavior to the presentation component or other container components. They call Flux actions and provide them as callbacks to the presentation component. Container components are often stateful because they are data sources (for other components)

18. What is the difference between a Class component and a Functional component?

Answer: 1. Class components not only allow you to use more additional functions, such as the component's own state and life cycle hooks, but also enable the component to directly access the store and maintain state

2. When a component only receives props and renders the component itself to the page, the component is a 'stateless component' and can be created using a pure function. Such components are also called dumb components or presentation components

19. What is the difference between state (state) and props (of a component)?

Answer: 1. State is a data structure used for the default value of the data required when the component is mounted. State may mutate over time, but most often as a result of user event behavior.

2. Props (abbreviation for properties) is the configuration of the component. Props are passed from parent components to child components, and as far as child components are concerned, props are immutable. A component cannot change its own props, but it can put the props of its subcomponents together (unified management). Props aren't just data either - callback functions can be passed via props too.

20. Where should the Ajax request be initiated in the React component?

Answer: In React components, network requests should be initiated in componentDidMount. This method will be executed when the component is first "mounted" (added to the DOM) and will only be executed once in the component's life cycle. More importantly, you are not guaranteed that the Ajax request has completed before the component is mounted, and if so, that means you will be trying to call setState on an unmounted component, which will not work. Making a network request in componentDidMount will ensure that there is a component ready to update.

21. What is a controlled component?

Answer; In HTML, form elements like ,

22. What is the role of refs in React?

Answer: Refs can be used to obtain a reference to a DOM node or React component. Good examples of when to use refs are to manage focus/text selection, trigger command animations, or integrate with third-party DOM libraries. You should avoid using String Refs and inline ref callbacks. Refs callbacks are recommended by React.

23. What is a higher order component?

Answer: A higher-order component is a function that takes a component as a parameter and returns a new component. HOC allows you to reuse code, logic, and bootstrapping abstractions. The most common one is probably Redux’s connect function. In addition to simply sharing tool libraries and simple composition, the best way for HOC is to share behaviors between React components. If you find that you have written a lot of code in different places to do the same thing, you should consider refactoring the code into a reusable HOC.

24. What are the advantages of using arrow functions?

Answer: 1. Scope safety: Before the arrow function, each newly created function has its own this value (in the constructor, it is a new object; in strict mode, in function calls, this is undefined; if the function is called an "object method", then the underlying object, etc.), but arrow functions don't, it uses the this value of the enclosing execution context.

2. Simple: Arrow functions are easy to read and write

3. Clear: When everything is an arrow function, any regular function can be used immediately to define the scope. Developers can always look for next-higher function statements to see the value of this

25. Why is it recommended that the parameter passed to setState is a callback instead of an object?

Answer: Because the updates of this.props and this.state may be asynchronous, their values ​​cannot be relied on to calculate the next state.

26. In addition to binding this in the constructor, is there any other way?

Answer: You can use property initializers to bind callbacks correctly, and create-react-app also supports it by default. In callbacks you can use arrow functions, but the problem is that a new callback is created every time the component renders.

27. How to prevent the rendering of components?

Answer: Returning null in the render method of the component will not affect the life cycle method of the triggered component

28. When rendering a list, what is the key? What is the purpose of setting key?

Answer: Keys will help React identify which items have changed, been added or removed. Keys should be assigned to elements within the array to give the (DOM) element a stable identity. The best way to choose a key is to use a string that uniquely identifies a list item. Many times you will use IDs in the data as keys. When you do not have stable IDs for the items being rendered, you can use the item index as the key for the rendered items, but this method is not recommended if the items can be reordered. This will cause re-render to slow down

29. What is the purpose of calling super(props) (in the constructor)?

Answer: Subclasses cannot use this before super() is called. In ES2015, subclasses must call super() in the constructor. The reason for passing props to super() is to facilitate access to this.props in the constructor (in subclasses).

30. What is JSX?

Answer: JSX is a syntax extension of JavaScript syntax and has all the functions of JavaScript. JSX produces React "elements", and you can wrap any JavaScript expression in curly braces and embed it in JSX. After compilation, JSX expressions become regular JavaScript objects, which means you can use JSX inside if statements and for loops, assign it to variables, accept it as an argument, and return it from functions.

31What are Children?

Answer: In JSX expressions, the content between an opening tag (such as ) and a closing tag (such as ) will be treated as a special attribute props.children Automatically passed to the containing component.

This property has many methods available, including React.Children.map, React.Children.forEach, React.Children.count, React.Children.only, React.Children.toArray.

32. In React, what is state?

Answer: State is similar to props, but it is private and completely controlled by the component itself. State is essentially an object that holds data and determines how the component is rendered.

33. What reasons would prompt you to break away from the dependency of create-react-app?

Answer: When you want to configure webpack or babel presets.

34. What is redux?

Answer: The basic idea of ​​Redux is to keep the entire application state in a single store. The store is a simple JavaScript object, and the only way to change the application state is to trigger actions in the application, and then write reducers for these actions to modify the state. The entire state transformation is completed in reducers and should not have any side effects

35. In Redux, what is a store?

Answer: Store is a javascript object that saves the state of the entire application. At the same time, Store also assumes the following responsibilities:

  1. Allow access to state through getState()
  2. Run state through dispatch(action)
  3. Through subscribe(listener) ) Register listeners
  4. Handle listeners’ notes through the function returned by subscribe(listener)

36. What is action?

Answer: Actions are a pure javascript object, they must have a type attribute indicating the type of action being executed. Essentially, an action is the payload that sends data from the application to the store.

37. What is reducer?

Answer: A reducer is a pure function that takes the previous state and an action as parameters and returns the next state.

38. What is the function of Redux Thunk?

Answer: Redux thunk is a middleware that allows you to write actions creators that return a function instead of an action. If a certain condition is met, thunk can be used to delay the dispatch of action (dispatch), which can handle the dispatch of asynchronous action (dispatch).

39. What is a pure function?

Answer: A pure function is a function that does not depend on and does not change the state of variables outside its scope. This also means that a pure function always returns the same result for the same parameters.

For more programming related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of Some common interview questions about React (share). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
react中canvas的用法是什么react中canvas的用法是什么Apr 27, 2022 pm 03:12 PM

在react中,canvas用于绘制各种图表、动画等;可以利用“react-konva”插件使用canvas,该插件是一个canvas第三方库,用于使用React操作canvas绘制复杂的画布图形,并提供了元素的事件机制和拖放操作的支持。

react中antd和dva是什么意思react中antd和dva是什么意思Apr 21, 2022 pm 03:25 PM

在react中,antd是基于Ant Design的React UI组件库,主要用于研发企业级中后台产品;dva是一个基于redux和“redux-saga”的数据流方案,内置了“react-router”和fetch,可理解为应用框架。

React是双向数据流吗React是双向数据流吗Apr 21, 2022 am 11:18 AM

React不是双向数据流,而是单向数据流。单向数据流是指数据在某个节点被改动后,只会影响一个方向上的其他节点;React中的表现就是数据主要通过props从父节点传递到子节点,若父级的某个props改变了,React会重渲染所有子节点。

react中为什么使用nodereact中为什么使用nodeApr 21, 2022 am 10:34 AM

因为在react中需要利用到webpack,而webpack依赖nodejs;webpack是一个模块打包机,在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,所以react需要使用nodejs。

react是组件化开发吗react是组件化开发吗Apr 22, 2022 am 10:44 AM

react是组件化开发;组件化是React的核心思想,可以开发出一个个独立可复用的小组件来构造应用,任何的应用都会被抽象成一颗组件树,组件化开发也就是将一个页面拆分成一个个小的功能模块,每个功能完成自己这部分独立功能。

react和reactdom有什么区别react和reactdom有什么区别Apr 27, 2022 am 10:26 AM

react和reactdom的区别是:ReactDom只做和浏览器或DOM相关的操作,例如“ReactDOM.findDOMNode()”操作;而react负责除浏览器和DOM以外的相关操作,ReactDom是React的一部分。

react中forceupdate的用法是什么react中forceupdate的用法是什么Apr 19, 2022 pm 12:03 PM

在react中,forceupdate()用于强制使组件跳过shouldComponentUpdate(),直接调用render(),可以触发组件的正常生命周期方法,语法为“component.forceUpdate(callback)”。

react与vue的虚拟dom有什么区别react与vue的虚拟dom有什么区别Apr 22, 2022 am 11:11 AM

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor