devmhimran / react-interview-questions

🟣 React Coding Interview Questions Answered to help you get ready for your next coding interview.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Top 161 React interview questions and answers in 2021.

You can check all 161 React interview questions here πŸ‘‰ https://devinterview.io/dev/react-interview-questions



πŸ”Ή 1. How does React work?

Answer:

React creates a virtual DOM. When state changes in a component it firstly runs a "diffing" algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.

Source: github.com/Pau1fitz   


πŸ”Ή 2. What is context?

Answer:

Context provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated user, locale preference, UI theme need to be accessed in the application by many components.

const {Provider, Consumer} = React.createContext(defaultValue);
Source: github.com/sudheerj   


πŸ”Ή 3. What is virtual DOM?

Answer:

The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the β€œreal” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

Source: github.com/sudheerj   


πŸ”Ή 4. What is props in ReactJS?

Answer:

Props are inputs to a React component. They are single values or objects containing a set of values that are passed to React Components on creation using a naming convention similar to HTML-tag attributes. i.e, 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 React 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,

 <Element reactProp = "1" />

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.

 props.reactProp;


πŸ”Ή 5. What is the use of refs?

Answer:

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

Source: github.com/sudheerj   


πŸ”Ή 6. What is JEST?

Answer:

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It's often used for testing React components.

Source: github.com/sudheerj   


πŸ”Ή 7. What are the advantages of ReactJS?

Answer:

Below are the advantages of ReactJS:

  1. Increases the application’s performance with Virtual DOM
  2. JSX makes code is easy to read and write
  3. It renders both on client and server side
  4. Easy to integrate with other frameworks (Angular, BackboneJS) since it is only a view library
  5. Easy to write UI Test cases and integration with tools such as JEST.
Source: github.com/sudheerj   


πŸ”Ή 8. What is ReactJS?

Answer:

ReactJS is an open-source frontend JavaScript library which is used for building user interfaces especifically 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. ReactJS was first deployed on Facebook’s newsfeed in 2011 and on Instagram.com in 2012.



πŸ”Ή 9. How to write comments in ReactJS?

Answer:

The comments in ReactJS/JSX is similar to javascript multiline comments which are wrapped with curly braces:

Single-line comments:

<div>
  {/* Single-line comments */}
  Welcome {user}, Let's play React
</div>

Multi-line comments:

<div>
  {/* Multi-line comments for more than
   one line */}
  Welcome {user}, Let's play React
</div>
Source: github.com/sudheerj   


πŸ”Ή 10. How would you write an inline style in React?

Answer:

For example:

<div style={{ height: 10 }}>
Source: github.com/WebPredict   


πŸ”Ή 11. What are the major features of ReactJS?

Answer:

The major features of ReactJS are as follows,

  • It uses VirtualDOM instead 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


πŸ”Ή 12. What are props in React?

Answer:

Props are properties that are passed into a child component from its parent, and are readonly.

Source: github.com/WebPredict   


πŸ”Ή 13. What are the differences between a class component and functional component?

Answer:

  • Class components allows you to use additional features such as local state and lifecycle hooks. Also, to enable your component to have direct access to your store and thus holds state.

  • When your component just receives props and renders them to the page, this is a stateless component, for which a pure function can be used. These are also called dumb components or presentational components.

Source: github.com/Pau1fitz   


πŸ”Ή 14. Where in a React component should you make an AJAX request?

Answer:

componentDidMount is where an AJAX request should be made in a React component.

This method will be executed when the component β€œmounts” (is added to the DOM) for the first time. This method is only executed once during the component’s life. Importantly, you can’t guarantee the AJAX request will have resolved before the component mounts. If it doesn't, that would mean that you’d be trying to setState on an unmounted component, which would not work. Making your AJAX request in componentDidMount will guarantee that there’s a component to update.

Source: github.com/Pau1fitz   


πŸ”Ή 15. What is the difference between state and props?

Answer:

The state is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.

Props (short for properties) are a Component's configuration. They are received from above and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data - callback functions may be passed in as props.

Source: github.com/Pau1fitz   


πŸ”Ή 16. What is the difference between a Presentational component and a Container component?

Answer:

  • Presentational components are concerned with how things look. They generally receive data and callbacks exclusively via props. These components rarely have their own state, but when they do it generally concerns UI state, as opposed to data state.

  • Container components are more concerned with how things work. These components provide the data and behavior to presentational or other container components. They call Flux actions and provide these as callbacks to the presentational components. They are also often stateful as they serve as data sources.

Source: github.com/Pau1fitz   


πŸ”Ή 17. What are refs used for in React?

Answer:

Refs are an escape hatch which allow you to get direct access to a DOM element or an instance of a component. In order to use them you add a ref attribute to your component whose value is a callback function which will receive the underlying DOM element or the mounted instance of the component as its first argument.

class UnControlledForm extends Component {
  handleSubmit = () => {
    console.log("Input Value: ", this.input.value)
  }
  render () {
    return (
      <form onSubmit={this.handleSubmit}>
        <input
          type='text'
          ref={(input) => this.input = input} />
        <button type='submit'>Submit</button>
      </form>
    )
  }
}

Above notice that our input field has a ref attribute whose value is a function. That function receives the actual DOM element of input which we then put on the instance in order to have access to it inside of the handleSubmit function.

It’s often misconstrued that you need to use a class component in order to use refs, but refs can also be used with functional components by leveraging closures in JavaScript.

function CustomForm ({handleSubmit}) {
  let inputElement
  return (
    <form onSubmit={() => handleSubmit(inputElement.value)}>
      <input
        type='text'
        ref={(input) => inputElement = input} />
      <button type='submit'>Submit</button>
    </form>
  )
}
Source: github.com/Pau1fitz   


πŸ”Ή 18. What's the difference between a controlled component and an uncontrolled one in React?

Answer:

  • A controlled component has its state completely driven by React,
  • Uncontrolled components can maintain their own internal state. E.g., a textarea's value.
Source: github.com/WebPredict   


πŸ”Ή 19. What are controlled components?

Answer:

A ReactJS component that controls the input elements within the forms on subsequent user input is called β€œControlled component”. i.e, every state mutation will have an associated handler function.

For example, to write all the names in uppercase letters, we use handleChange as below,

handleChange(event) {
    this.setState({
        value: event.target.value.toUpperCase()
    });
}
Source: github.com/sudheerj   


πŸ”Ή 20. What is state in ReactJS?

Answer:

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 user component with message state,

class User extends React.Component {
   constructor(props) {
      super(props);
  <span class="token cVar">this</span><span class="token cBase">.</span>state <span class="token cBase">=</span> <span class="token cBase">{</span>
     message<span class="token cBase">:</span> <span class="token cString">"Welcome to React world"</span><span class="token cBase">,</span>
  <span class="token cBase">}</span>

} render() { return ( <div> <h1>{this.state.message}</h1> </div> ); } }



πŸ”Ή 21. When to use a Class Component over a Functional Component?

Answer:

If the component need state or lifecycle methods then use class component otherwise use functional component.



πŸ”Ή 22. What does it mean for a component to be mounted in React?

Answer:

It has a corresponding element created in the DOM and is connected to that.

Source: github.com/WebPredict   


πŸ”Ή 23. How do we pass a property from a parent component props to a child component?

Answer:

For example:

<ChildComponent someProp={props.someProperty} />
Source: github.com/WebPredict   


πŸ”Ή 24. What are fragments?

Answer:

It's common pattern in React which is used for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.

render() {
return (
<React.Fragment>
<ChildA />
<ChildB />
<ChildC />
</React.Fragment>
);
}

There is also a shorter syntax which is not supported in many tools

render() {
return (
<>
<ChildA />
<ChildB />
<ChildC />
</>
);
}
Source: github.com/sudheerj   


πŸ”Ή 25. When rendering a list what is a key and what is it's purpose?

Answer:

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity. The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.

render () {
return (
<ul>
{this.state.todoItems.map(({task, uid}) => {
return <li key={uid}>{task}</li>
})}
</ul>
)
}

Most often you would use IDs from your data as keys. When you don't have stable IDs for rendered items, you may use the item index as a key as a last resort. It is not recommend to use indexes for keys if the items can reorder, as that would be slow.

Source: github.com/Pau1fitz   


πŸ”Ή 26. How to create refs?

Answer:

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 with in constructor.

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

And:

class UserForm extends Component {
handleSubmit = () => {
console.log("Input Value is: ", this.input.value)
}
render () {
return (
<form onSubmit={this.handleSubmit}>
<input
type='text'
ref={(input) => this.input = input} /> // Access DOM input in handle submit
<button type='submit'>Submit</button>
</form>
)
}
}

We can also use it in functional components with the help of closures.

Source: github.com/sudheerj   


πŸ”Ή 27. What happens when you call setState?

Answer:

The state property is updated in a React component with the object passed into setState, and this is done asynchronously. It tells React that this component and its children need to be re-rendered, but React may not do this immediately (it may batch these state update requests for better performance).

Source: github.com/WebPredict   


πŸ”Ή 28. What are stateful components?

Answer:

If the behaviour of a component is dependent on the state of the component then it can be termed as stateful component. These Stateful components are always class components and have a state that gets initialized in the constructor.

class App extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}

render() { // omitted for brevity } }

Source: github.com/sudheerj   


πŸ”Ή 29. How would you prevent a component from rendering in React?

Answer:

Return null from the render method.

Source: github.com/WebPredict   


πŸ”Ή 30. What is JSX?

Answer:

JSX is a syntax notation for JavaScript XML(XML-like syntax extension to ECMAScript). It stands for JavaScript XML. It provides expressiveness of JavaScript along with HTML like template syntax. For example, the below text inside h1 tag return as javascript function to the render function,

   render(){
return(
<div>
<h1> Welcome to React world!!</h1>
</div>
);
}


πŸ”Ή 31. How error boundaries handled in React (15)?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 32. Where is the state kept in a React + Redux application?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 33. What are the limitations of ReactJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 34. What is the difference between React Native and React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 35. What are stateless components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 36. How is React different from AngularJS (1.x)?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 37. What is the point of Redux?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 38. Why is it necessary to capitalize the components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 39. What is the difference between state and props?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 40. What is Flow?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 41. How to create components in ReactJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 42. What is the purpose of callback function as an argument of setState?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 43. What are portals in ReactJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 44. How to pass a parameter to an event handler or callback?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 45. What’s the difference between an "Element" and a "Component" in React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 46. What happens during the lifecycle of a React component?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 47. What is Flux?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 48. What is the difference between component and container in react Redux?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 49. What is inline conditional expressions?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 50. How do you prevent the default behavior in an event callback in React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 51. What is reconciliation?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 52. What is the purpose of using super constructor with props argument?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 53. What happens when you call "setState"?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 54. Describe how events are handled in React.

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 55. What is the difference between Element and Component?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 56. What are Higher-Order components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 57. Name the different lifecycle methods.

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 58. What is a higher order component?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 59. What is JSX?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 60. What is state in react?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 61. What are controlled components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 62. What is a store in redux?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 63. How would you prevent a component from rendering?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 64. What don't you like about React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 65. What advantages are using arrow functions?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 66. Why is it advised to pass a callback function to setState as opposed to an object?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 67. What's the typical pattern for rendering a list of components from an array of data?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 68. What are PropTypes in React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 69. What are Pure Components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 70. What are the advantages of React over VueJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 71. Name some popular Flux Libraries

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 72. What's the typical flow of data like in a React + Redux app?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 73. What are synthetic events in ReactJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 74. What's an alternative way to avoid having to bind to this in event callback methods?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 75. What are some limitations of things you shouldn't do in the component's render method?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 76. How to bind methods or event handlers in JSX callbacks?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 77. What is useState() in React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 78. What is the difference between createElement and cloneElement?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 79. Why fragments are better than container divs?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 80. What is prop drilling and how can you avoid it?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 81. What is an action?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 82. What is the point of shouldComponentUpdate() method?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 83. What are forward refs?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 84. What do these three dots (...) in React do?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 85. How do you tell React to build in Production mode and what will that do?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 86. What are typical middleware choices for handling asynchronous calls in Redux?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 87. What are the lifecycle methods of ReactJS?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 88. What are the different phases of ReactJS component lifecycle?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 89. What is Key and benefit of using it in lists?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 90. What's the difference between an Element and a Component in React?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 91. What is the difference between ShadowDOM and VirtualDOM?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 92. Why do class methods need to be bound to a class instance?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 93. What are stateless components?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 94. What is ReactDOM?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 95. What is children prop?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 96. Why ReactJS uses className over class attribute?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 97. What are React Hooks?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 98. What does "shouldComponentUpdate" do and why is it important?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 99. What's the difference between a "smart" component and a "dumb" component?

πŸ‘‰πŸΌ Check all 161 answers


πŸ”Ή 100. What is the render method for?

πŸ‘‰πŸΌ Check all 161 answers



Thanks πŸ™Œ for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here πŸ‘‰ Devinterview.io

About

🟣 React Coding Interview Questions Answered to help you get ready for your next coding interview.