Change language

React Interview Questions

If you want to get a great job at React in 2020 or 2021, you’ve come to the right place.

We bring you this post on the back of our new React Interview Questions module with the amazing @Cassidoo ?

In this course, Cassidoo draws on her work experience at Netlify (and before that at CodePen) to share 26 possible React interview questions and sample answers.

You’re reading an epic 4,500 word version of the same common React interview questions and sample answers. Use it as a quick reference or as an exercise to rehearse your answers aloud. We have also included a PDF below in case you want to download and print something ?.

Here we list the same questions plus verified answers so you can customize them. Use this as inspiration to formulate eloquent and confident answers that will surprise your future employer.


1 What is React?
2 What are the major features of React?
3 What is JSX?
4 What is the difference between Element and Component?
5 How to create components in React?
6 When to use a Class Component over a Function Component?
7 What are Pure Components?
8 What is state in React?
9 What are props in React?
10 What is the difference between state and props?
11 Why should we not update the state directly?
12 What is the purpose of callback function as an argument of setState()?
13 What is the difference between HTML and React event handling?
14 How to bind methods or event handlers in JSX callbacks?
15 How to pass a parameter to an event handler or callback?
16 What are synthetic events in React?
17 What are inline conditional expressions?
18 What is "key" prop and what is the benefit of using it in arrays of elements?
19 What is the use of refs?
20 How to create refs?
21 What are forward refs?
22 Which is preferred option with in callback refs and findDOMNode()?
23 Why are String Refs legacy?
24 What is Virtual DOM?
25 How Virtual DOM works?
26 What is the difference between Shadow DOM and Virtual DOM?
27 What is React Fiber?
28 What is the main goal of React Fiber?
29 What are controlled components?
30 What are uncontrolled components?

Core React

  1. What is React?

    While this sounds like a relatively simple question, it actually asks the candidate to provide an informed opinion about React as well as competing alternatives. In short, this question is designed to test a candidate’s knowledge of the JavaScript ecosystem as a whole, while also asking for specifics about what makes React unique. Let’s look at each part of the answer separately.

    The gist of this answer is that the main purpose of React is to create UI components. it is often just referred to as a "V" (View) in an "MVC" architecture. Hence, it has no opinions on the other parts of your technology stack and can be seamlessly integrated into any application.

    React is an open-source frontend JavaScript library which is used for building user interfaces especially for single page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. React was first deployed on Facebook’s News Feed in 2011 and on Instagram in 2012.

    Back to Top

  2. What are the major features of React?

    The major features of React are:

    • It uses VirtualDOM instead of RealDOM considering that RealDOM manipulations are expensive.
    • Supports server-side rendering.
    • Follows Unidirectional data flow or data binding.
    • Uses reusable/composable UI components to develop the view.

    Back to Top

  3. What is JSX?

    JSX is a XML-like syntax extension to ECMAScript (the acronym stands for JavaScript XML). Basically it just provides syntactic sugar for the React.createElement() function, giving us expressiveness of JavaScript along with HTML like template syntax.

    In the example below text inside <h1> tag is returned as JavaScript function to the render function.

    Back to Top

  4. React interview questions
  5. What is the difference between Element and Component?

    An Element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other Elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.

    The object representation of React Element would be as follows:

    The above React.createElement() function returns an object:

    And finally it renders to the DOM using ReactDOM.render():

    Whereas a component can be declared in several different ways. It can be a class with a render() method or it can be defined as a function. In either case, it takes props as an input, and returns a JSX tree as the output:

    Then JSX gets transpiled to a React.createElement() function tree:

    Back to Top

  6. React Interview Questions 2021
  7. How to create components in React?

    There are two possible ways to create a component.

    1. Function Components: This is the simplest way to create a component. Those are pure JavaScript functions that accept props object as first parameter and return React elements:

    2. Class Components: You can also use ES6 class to define a component. The above function component can be written as:

    Back to Top

  8. When to use a Class Component over a Function Component?

    If the component needs state or lifecycle methods then use class component otherwise use function component. However, from React 16.8 with the addition of Hooks, you could use state , lifecycle methods and other features that were only available in class component right in your function component.

    Back to Top

  9. What are Pure Components?

    React.PureComponent is exactly the same as React.Component except that it handles the shouldComponentUpdate() method for you. When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won’t compare current props and state to next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.

    Back to Top

  10. What is state in React?

    State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.

    Let’s create a user component with message state,

    state

    State is similar to props, but it is private and fully controlled by the component. i.e, It is not accessible to any other component til the owner component decides to pass it.

    Back to Top

  11. What are props in React?

    Props are inputs to components. They are single values or objects containing a set of values that are passed to components on creation using a naming convention similar to HTML-tag attributes. They are data passed down from a parent component to a child component.

    The primary purpose of props in React is to provide following component functionality:

    1. Pass custom data to your component.
    2. Trigger state changes.
    3. Use via this.props.reactProp inside component’s render() method.

    For example, let us create an element with reactProp property:

    This reactProp (or whatever you came up with) name then becomes a property attached to React’s native props object which originally already exists on all components created using React library.

    Back to Top

  12. What is the difference between state and props?

    Both props and state are plain JavaScript objects. While both of them hold information that influences the output of render, they are different in their functionality with respect to component. Props get passed to the component similar to function parameters whereas state is managed within the component similar to variables declared within a function.

Back to Top

  1. Why should we not update the state directly?

    If you try to update the state directly then it won’t re-render the component.

    Instead use setState() method. It schedules an update to a component’s state object. When state changes, the component responds by re-rendering.

    Note: You can directly assign to the state object either in constructor or using latest javascript’s class field declaration syntax.

Back to Top

  1. What is the purpose of callback function as an argument of setState()?

    The callback function is invoked when setState finished and the component gets rendered. Since setState() is asynchronous the callback function is used for any post action.

    Note: It is recommended to use lifecycle method rather than this callback function.

Back to Top

  1. What is the difference between HTML and React event handling?

    Below are some of the main differences between HTML and React event handling,

    1. In HTML, the event name usually represents in lowercase as a convention:

      Whereas in React it follows camelCase convention:

    2. In HTML, you can return false to prevent default behavior:

      Whereas in React you must call preventDefault() explicitly:

    3. In HTML, you need to invoke the function by appending () Whereas in react you should not append () with the function name. (refer "activateLasers" function in the first point for example)

Back to Top

  1. How to bind methods or event handlers in JSX callbacks?

    There are 3 possible ways to achieve this:

    1. Binding in Constructor: In JavaScript classes, the methods are not bound by default. The same thing applies for React event handlers defined as class methods. Normally we bind them in constructor.

    2. Public class fields syntax: If you don’t like to use bind approach then public class fields syntax can be used to correctly bind callbacks.

    3. Arrow functions in callbacks: You can use arrow functions directly in the callbacks.

    Note: If the callback is passed as prop to child components, those components might do an extra re-rendering. In those cases, it is preferred to go with .bind() or public class fields syntax approach considering performance.

Back to Top

  1. How to pass a parameter to an event handler or callback?

    You can use an arrow function to wrap around an event handler and pass parameters:

    This is an equivalent to calling .bind:

    Apart from these two approaches, you can also pass arguments to a function which is defined as arrow function

Back to Top

  1. What are synthetic events in React?

    SyntheticEvent is a cross-browser wrapper around the browser’s native event. It’s API is same as the browser’s native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

Back to Top

  1. What are inline conditional expressions?

    You can use either if statements or ternary expressions which are available from JS to conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator &&.

Back to Top

  1. What is "key" prop and what is the benefit of using it in arrays of elements?

    A key is a special string attribute you should include when creating arrays of elements. Key prop helps React identify which items have changed, are added, or are removed.

    Most often we use ID from our data as key:

    When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:

    Note:

    1. Using indexes for keys is not recommended if the order of items may change. This can negatively impact performance and may cause issues with component state.
    2. If you extract list item as separate component then apply keys on list component instead of li tag.
    3. There will be a warning message in the console if the key prop is not present on list items.

Back to Top

  1. What is the use of refs?

    The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.

Back to Top

  1. How to create refs?

    There are two approaches

    1. This is a recently added approach. Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property within constructor.

    2. You can also use ref callbacks approach regardless of React version. For example, the search bar component’s input element accessed as follows,

    You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach

Back to Top

  1. Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically