35 interview questions you must know and master React

青灯夜游
Release: 2020-09-02 15:33:56
forward
21084 people have browsed it

35 interview questions you must know and master React

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

Question 1: What is Virtual DOM?

Theme: React
Difficulty: ⭐

Virtual DOM (VDOM)is an in-memory representation of the real DOM. The representation of the UI is kept in memory and synchronized with the actual DOM. This is a step that occurs between the rendering function being called and the element being displayed on the screen. The entire process is calledreconciliation.

Question 2: What is the difference between class components and function components?

Theme: React
Difficulty: ⭐⭐
  • ##Class componentCan use other features such as statestateand lifecycle hooks.
  • When a component only receives
  • propsand renders it to the page, it is a stateless component and is a functional component. It is also called a dumb component or a display component.
Of course there is a difference between function components and class components, and the performance of function components is higher than that of class components, because class components need to be instantiated when used, while function components directly execute function fetching Just return the result. To improve performance, try to use functional components.

Question 3: Why are refs used in React?

Theme: React
Difficulty: ⭐⭐

RefsProvides a way to access the DOM created in therendermethod Methods of nodes or React elements. In a typical data flow,propsis the only way for parent and child components to interact. If you want to modify a child component, you need to re-render it with a newpros. There are exceptions to everything. In some cases, we need to force modification of children outside of the typical data flow. In this case,Refscan be used.

We can add arefattribute to the component to use. The value of this attribute is a callback function that receives the underlying DOM element or the mounted instance of the component as its first parameter.

class UnControlledForm extends Component { handleSubmit = () => { console.log("Input Value: ", this.input.value) } render () { return ( 
this.input = input} />
) } }
Copy after login

Please note that theinputelement has arefattribute whose value is a function. This function receives the actual DOM element of the input and then places it on the instance so that it can be accessed inside thehandleSubmitfunction.

It is often misunderstood thatrefscan only be used within class components, butrefscan also be used with function components by leveraging closures in JS.

function CustomForm ({handleSubmit}) { let inputElement return ( 
handleSubmit(inputElement.value)}> inputElement = input} />
) }
Copy after login

Question 4: How to handle events in React

Topic: React
Difficulty: ⭐⭐

In order to solve cross-browser issues Compatibility issue,SyntheticEventinstance will be passed to your event handler function,SyntheticEventis React's cross-browser browser native event wrapper, it also has browser native events The same interface, includingstopPropagation()andpreventDefault().

What’s interesting is that React doesn’t actually attach events to the child nodes themselves. React uses a single event listener to listen to all events at the top level. This is good for performance and means React doesn't need to track event listeners when updating the DOM.

Question 5: What is the difference between state and props?

Theme: React
Difficulty: ⭐⭐

propsandstateare ordinary JS objects. Although they both contain information that affects rendered output, their functionality in terms of components is different. That is,

  • stateis the component’s own data management, control of its own state, and is variable;
  • propsis the data passed in from the outside. Parameters, immutable;
  • Those withoutstateare called stateless components, and those withstateare called stateful components;
  • Useprops more, use lessstate, that is, write more stateless components.

Question 6: How to create refs

Theme: React
Difficulty: ⭐⭐

Refs are created usingReact .createRef()Created and attached to the React element via therefattribute. When constructing a component, you typically assignRefsto instance properties so that they can be referenced throughout the component.

class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return 

; } }

Copy after login

Or use it like this:

class UserForm extends Component { handleSubmit = () => { console.log("Input Value is: ", this.input.value) } render () { return ( 
this.input = input} /> // Access DOM input in handle submit
) } }
Copy after login

Question 7: What are higher-order components?

Theme: React
Difficulty: ⭐⭐

Higher Order Component (HOC)is a function that accepts a component and returns a new component. Basically, this is a pattern derived from the composition feature of React, calledPure Componentsbecause they can accept any dynamically provided subcomponent, but do not modify or copy the input component any behavior in.

const EnhancedComponent = higherOrderComponent(WrappedComponent);
Copy after login

HOC can be used for many of the following use cases

  • Code reuse, logic and bootstrapping abstractions
  • Render hijacking
  • state abstractions and operations
  • props processing

Question 8: What is the role of callingsuperin the constructor and passingpropsas a parameter?

Topic: React
Difficulty: ⭐⭐

The subclass constructor cannot be used until thesuper()method is calledthisQuotes, the same goes for ES6 subclasses. The main reason for passingpropsparameters to thesuper()call is to be able to obtain the incomingprops throughthis.propsin the child constructor.

Passing props

class MyComponent extends React.Component { constructor(props) { super(props); console.log(this.props); // { name: 'sudheer',age: 30 } } }
Copy after login

Not passing props

class MyComponent extends React.Component { constructor(props) { super(); console.log(this.props); // undefined // 但是 Props 参数仍然可用 console.log(props); // Prints { name: 'sudheer',age: 30 } } render() { // 构造函数外部不受影响 console.log(this.props) // { name: 'sudheer',age: 30 } } }
Copy after login

The above example reveals something. The behavior ofpropsis different only inside the constructor, it is the same outside the constructor.

Question 9: What is a control component?

Theme: React
Difficulty: ⭐⭐⭐

In HTML, form elements such as,and ); } });

Copy after login

渲染结果:

Copy after login

另一种可能的方法是:

var condition = true; var component = ( 

);

Copy after login

问题 31:Hooks会取代render props和高阶组件吗?

主题: React
难度: ⭐⭐⭐⭐

通常,render props和高阶组件仅渲染一个子组件。React团队认为,Hooks 是服务此用例的更简单方法。

这两种模式仍然有一席之地(例如,一个虚拟的scroller组件可能有一个renderItem prop,或者一个可视化的容器组件可能有它自己的 DOM 结构)。但在大多数情况下,Hooks 就足够了,可以帮助减少树中的嵌套。

问题 32:如何避免组件的重新渲染?

主题: React
难度: ⭐⭐⭐⭐

React 中最常见的问题之一是组件不必要地重新渲染。React 提供了两个方法,在这些情况下非常有用:

  • React.memo():这可以防止不必要地重新渲染函数组件
  • PureComponent:这可以防止不必要地重新渲染类组件

这两种方法都依赖于对传递给组件的props的浅比较,如果props没有改变,那么组件将不会重新渲染。虽然这两种工具都非常有用,但是浅比较会带来额外的性能损失,因此如果使用不当,这两种方法都会对性能产生负面影响。

通过使用React Profiler,可以在使用这些方法前后对性能进行测量,从而确保通过进行给定的更改来实际改进性能。

问题 33:什么是纯函数?

主题: React
难度: ⭐⭐⭐⭐⭐

纯函数是不依赖并且不会在其作用域之外修改变量状态的函数。本质上,纯函数始终在给定相同参数的情况下返回相同结果。

问题 34:当调用setState时,Reactrender是如何工作的?

主题: React
难度: ⭐⭐⭐⭐⭐

咱们可以将"render"分为两个步骤:

  1. 虚拟 DOM 渲染:当render方法被调用时,它返回一个新的组件的虚拟 DOM 结构。当调用setState()时,render会被再次调用,因为默认情况下shouldComponentUpdate总是返回true,所以默认情况下 React 是没有优化的。
  2. 原生 DOM 渲染:React 只会在虚拟DOM中修改真实DOM节点,而且修改的次数非常少——这是很棒的React特性,它优化了真实DOM的变化,使React变得更快。

问题 35:如何避免在React重新绑定实例?

主题: React
难度: ⭐⭐⭐⭐⭐

有几种常用方法可以避免在 React 中绑定方法:

1.将事件处理程序定义为内联箭头函数

class SubmitButton extends React.Component { constructor(props) { super(props); this.state = { isFormSubmitted: false }; } render() { return (  ) } }
Copy after login

2.使用箭头函数来定义方法:

class SubmitButton extends React.Component { state = { isFormSubmitted: false } handleSubmit = () => { this.setState({ isFormSubmitted: true }); } render() { return (  ) } }
Copy after login

3.使用带有 Hooks 的函数组件

const SubmitButton = () => { const [isFormSubmitted, setIsFormSubmitted] = useState(false); return (  ) };
Copy after login
本文转载自:https://segmentfault.com/a/1190000020912300

相关教程推荐:React视频教程

Difference Function component Class component Is there No Yes Is there a life cycle No Yes ##Is there a state state
this
No Yes

The above is the detailed content of 35 interview questions you must know and master React. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!