10X Sale
kh logo
All Courses

Introduction

React is an open-source JavaScript framework and library that is used to build an interactive user interface and web applications quicker with lesser code. React is a front-end JavaScript library capable of making API calls that deal with the data. Be it a beginner or an intermediate or an experienced React professional, this guide will aid you in increasing your confidence and knowledge in React. The questions below are compiled by experts that will help you prepare for your React Interview. Alongside, it will also provide step-by-step explanations for each question which will help you understand the concepts in detail. With React interview questions by your side, you can be positive about your preparation and ace the upcoming interview.

React Interview Questions and Answers
Beginner

1. What are Controlled Components in React?

Controlled components are React components that control their state via props. The state of the form elements is handled by React rather than the DOM.

Example:

    class ControlledComponent extends React.Component { 
      constructor(props) { 
        super(props); 
        this.state = { value: '' }; 
      }
      handleChange = (event) => { 
        this.setState({ value: event.target.value }); 
      }
      render() { 
        return ( 
          <input  
            type="text"  
            value={this.state.value}  
            onChange={this.handleChange}  
          /> 
        ); 
      } 
    } 

2. What are Uncontrolled Components in React?

Uncontrolled components rely on the DOM to maintain their state. You can access the current value of the element using refs. 

Example:

class UncontrolledComponent extends React.Component {
      constructor(props) {
        super(props);
        this.inputRef = React.createRef();
      }
      handleSubmit = (event) => {
        event.preventDefault();
        alert(this.inputRef.current.value);
      }
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            <input type="text" ref={this.inputRef} />
            <button type="submit">Submit</button>
          </form>
        );
      }
    }

3. What is the use of key in React and why is it important?

Key is a special string attribute we need to include when creating lists of elements. Keys help React identify which items have changed, are added, or are removed. 

Example:

const listItems = items.map((item) =>
      <li key={item.id}>{item.name}</li>
    );

 

4. How do you handle forms in React?

Forms in React can be handled by using controlled components where the component's state handles form data. 

Example:

class MyForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = { name: '' };
      }
      handleChange = (event) => {
        this.setState({ name: event.target.value });
      }
      handleSubmit = (event) => {
        event.preventDefault();
        alert('A name was submitted: ' + this.state.name);
      }
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Name:
              <input type="text" value={this.state.name} onChange={this.handleChange} />
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
    }

5. How do you conditionally r ender components in React?

You can conditionally render components using JavaScript conditional statements like if, else, or ternary operators within JSX. 

Example:

const MyComponent = ({ isLoggedIn }) => {
      return (
        <div>
          {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
        </div>
      );
    }

Want to Know More?
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Description

React is a Javascript Library that is used for building UI. It is maintained by Facebook and an individual group of developers. In the 2017 developer survey, Stack overflow noted that React is still among the most popular Javascript libraries. The main advantage of React is it is simple, scalable, and fast.

Today more than 94,000 sites and 1,300 developers are utilizing React. Fortune-500 companies like Facebook, PayPal, Uber, Instagram, and Airbnb utilize this JavaScript-based UI library for a major part of their application development efforts on web as well as mobile. Recently, React has got a lot of popularity among the top tech companies across the globe especially in India. As per Indeed, a React Developer can expect to earn an average salary of $108784 per year, making it a lucrative skill to learn through a React course.

If you’re looking for React interview questions and answers for experienced and freshers, then you are at the right place. There are a lot of opportunities in many reputed companies across the globe. Good hands-on knowledge concepts will put you forward in the interview. You can find job opportunities everywhere. Ours React interview questions are exclusively designed for supporting employees in clearing interviews. We have tried to cover almost all the main topics related to React.

Here, we have characterized the questions based on the level of expertise you’re looking for. Preparing for your interview with these interview questions on React will give you an edge over other interviewees and will help you crack the interview.

Recommended Courses

Learners Enrolled For
CTA
Got more questions? We've got answers.
Book Your Free Counselling Session Today.