Home  >  Article  >  Web Front-end  >  (Collection) Introduction to common interview questions in React

(Collection) Introduction to common interview questions in React

不言
不言forward
2018-10-22 17:35:021810browse

This article brings you an introduction to (collection of) commonly used interview questions in React. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The following is a list of commonly used interview questions about React:

Whether you are an interviewer or a recruiter, you can refer to the following questions

Be sure to bookmark

How React works

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.

What are the advantages of using React

You can easily know how a component is rendered by just looking at the render function

The introduction of JSX makes the component The code is more readable, and it is easier to understand the layout of components, or how components refer to each other

Supports server-side rendering, which can improve SEO and performance

Easy to test

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

Presentational component and Container component What's the difference between

Presentation components care about what the component looks 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.

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

What is the difference between Class component and Functional component

Class components not only allow you to use more additional functions, such as the component itself State and life cycle hooks also enable components to directly access the store and maintain state

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

What is the difference between (component's) state and properties (props)

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.

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.

Point out the difference in (component) life cycle methods

componentWillMount -- mostly used for application configuration in the root component

componentDidMount -- everything can be done here All configurations that cannot be done without DOM and start getting all the data you need; if you need to set up event listening, you can also complete it here

componentWillReceiveProps -- This periodic function acts on the state caused by a specific prop change Transition

shouldComponentUpdate -- If you're worried about over-rendering your component, shouldComponentUpdate is a place to improve performance because it prevents the component from re-rendering if it receives new props. shouldComponentUpdate should return a boolean value to determine whether the component should be re-rendered

componentWillUpdate -- rarely used. It can be used to replace the component's componentWillReceiveProps and shouldComponentUpdate (but cannot access the previous props)

componentDidUpdate -- often used to update the DOM in response to prop or state changes

componentWillUnmount -- here you You can cancel the network request, or remove all event listeners related to the component

Where should the Ajax request be initiated in the React component

In the React component, the network request 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 can't guarantee that before the component is mounted The Ajax request has completed. If so, it means you are 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.

What is a controlled component

In HTML, form elements like ,