Home > Article > Web Front-end > (Collection) Introduction to common interview questions in React
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
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.
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)
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).
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
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.
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
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.
In HTML, form elements like ,
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.
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.
Exercise
Write a HOC that reverses its input
Write a HOC that provides data from the API to the incoming component
Write a HOC that implements shouldComponentUpdate To avoid reconciliation HOC
Write a HOC that sorts the child components of the incoming component through React.Children.toArray
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, this in the function call 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.
Simple: Arrow functions are easy to read and write
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
Because the updates of this.props and this.state may be asynchronous, their values cannot be relied on to calculate the next state.
this
in the constructor, is there any other way?You can use property initializers to bind the callback correctly , create-react-app is also supported by default. In callbacks you can use arrow functions, but the problem is that a new callback is created every time the component renders.
Returning null in the render method of the component will not affect the life cycle method of the triggered component
Keys will help React identify which items have changed, been added or removed. Keys The elements within the array should be assigned to give the (DOM) element a stable identity, choose a key The best approach is to use a string that uniquely identifies a list item. Many times you will use IDs in the data as keys, when you don't have stable IDs When used with rendered items, you can use the item index as the key to render the item, but this is not recommended because if items can be reordered, it will cause re-rendering to be slower.
Before super() is called, subclasses cannot use this. 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).
JSX is a syntax extension of JavaScript syntax and has all the functions of JavaScript. JSX Production React "Element", you can wrap any JavaScript expression in curly braces and embed it in JSX. After compilation is complete, the JSX expression becomes a regular JavaScript object, which means you can use JSX inside if statements and for loops, assign it to a variable, accept it as an argument, and return it from functions.
Question:
const element = ( <h1 className="greeting"> Hello, rdhub.cn! </h1> );
Answer:
const element = React.createElement( 'h1', {className: 'greeting'}, 'Hello, rdhub.cn!' );
Children
In JSX expressions, the content between an opening tag (such as ) and a closing tag (such as ) will be automatically passed to props.children as a special attribute Contains its components.
This property has many methods available, including React.Children.map, React.Children.forEach, React.Children.count, React.Children.only, React.Children.toArray.
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.
When you want to configure webpack or babel presets.
The basic idea of Redux is to keep the entire application state in a single store. 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 state. The entire state transformation is done in reducers and should not have any side effects.
Store is a javascript object that saves the state of the entire application. At the same time, Store also assumes the following responsibilities:
Allow access to state through getState()
Run state through dispatch(action)
Register listeners through subscribe(listener)
Handle the logout of listeners through the function returned by subscribe(listener)
Actions is a pure javascript object, they must have a type attribute indicating that they are being executed The type of action. Essentially, an action is the payload that sends data from the application to the store.
A reducer is a pure function that takes the previous state and an action as parameters and returns the next state.
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).
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 The same parameters always return the same results.
The above is the detailed content of (Collection) Introduction to common interview questions in React. For more information, please follow other related articles on the PHP Chinese website!