search
HomeWeb Front-endJS Tutorial(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

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 ,

In React, what is the role of refs

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.

What is a higher order component?

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

What are the advantages of using arrow functions

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.

In addition to binding 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.

How to prevent the rendering of the component

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

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

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.

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

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).

What is JSX

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.

How to use React.createElement to rewrite the following code

Question:

const element = (
  <h1>
    Hello, rdhub.cn!
  </h1>
);

Answer:

const element = React.createElement(
  'h1',
  {className: 'greeting'},
  'Hello, rdhub.cn!'
);

What is 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.

In React, what is state

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.

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

When you want to configure webpack or babel presets.

What is redux

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.

In Redux, what is store

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)

What is action

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.

What is a reducer

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

What is the role of Redux Thunk

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).

What is a pure function?

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!

Statement
This article is reproduced at:segmentfault思否. If there is any infringement, please contact admin@php.cn delete
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.