Git Product home page Git Product logo

react-interview-questions-1's Introduction

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;
Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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.

Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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
Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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> ); } }

Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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.

Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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>
);
}
Source:ย https://github.com/sudheerjย  ย 


๐Ÿ”น 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

react-interview-questions-1's People

Contributors

devinterview-io avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.