Background

React is a one of the popular tool for building UI components using front-end JavaScript library. React is also known as React.js or ReactJS.

Important reasons, why to read this article?

In this article, questions having options. Correct answers are given along with detailed description and code examples. Hence, one can have actual idea about the concept.

Questions

  1. Which is recommended way to handle events in React?

  2. What is state in React?

  3. What does ES6 means?

  4. Which are common ways to handle data in React?

  5. Which is used to pass data to component from outside in React?

  6. What is virtual DOM in React?

  7. What is Babel?

  8. What is purpose of keys in React lists?

  9. Which method is used to initialize state in React component?

  10. What is everything in React?

  11. What is JSX in React?

  12. Which function is called to render HTML to web page in React?

  13. What is default port where webpack server runs?

  14. How to conditionally render components in React?

  15. Which command is used to install create-react-app globally?

Answers

1. Which is recommended way to handle events in React?

Correct Answer:

Explanation:

“Passing callback functions as props” is a common and recommended way to handle events, especially when child component needs to communicate with a parent components. This is achieved by passing callback functions as props.

Using it parent component can control state and behavior. Child component can trigger actions without managing logic themselves. It makes clear and predictable data flow from top to down.

JSX

function Parent() {
  const handleClick = () => {
    console.log("Button clicked!");
  };
  return <Child onButtonClick={handleClick} />;
}

function Child({ onButtonClick }) {
  return <button onClick={onButtonClick}>Click me</button>;
}

Why other options are incorrect?

2. What is state in React?

Correct Answer:

Explanation:

State refers to the data which is managed inside component and can change over time. React automatically re-renders a component when stated changes to reflect the updated data. State is local and internal for the component. It is mutable so it can change. It controls component behaviour and what it can render.

Why other options are incorrect?

3. What does ES6 means?

Correct Answer:

Explanation:

ES6 means ECMAScript 6 and also known as ES2015. It is 6th major version of ECMAScript standards. This specifications defines how JavaScript works.

let and const, arrow functions (=>) development, classes feature, template literals implementation, destructuring feature, and modules implementation, etc. are key features of ES6.

Why other options are incorrect?

4. Which are common ways to handle data in React?

Correct Answer:

Explanation:

In React, data is mainly handled using State and Props.

State:

Props:

JSX

function Child(props) {
  return <h1>{props.message}</h1>;
}

function Parent() {
  const message = "Hello React!";
  return <Child message={message} />;
}

Why other options are incorrect?

5. Which is used to pass data to component from outside in React?

Correct Answer:

Explanation:

props is short form of properties. It is used to pass data to a component from outside, generally from parent component to child component. It is read-only. It allow components to be reusable. It enables one-way data flow such as from parent to child.

JSX

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

function App() {
  return <Greeting name="React" />;
}

Why other options are incorrect?

6. What is virtual DOM in React?

Correct Answer:

Explanation:

The Virtual DOM is a lightweight and in-memory representation of real DOM. It is used efficiently to determine what changes need to be made to the actual DOM. React updates Virtual DOM while state or props change. It compares new Virtual DOM with previous one (that process is called diffing). Only necessary changes are applied to real DOM which improves performance.

Why other options are incorrect?

7. What is Babel?

Correct Answer:

Explanation:

Babel is a JavaScript transpiler. It converts or transpiles the modern JavaScript (i.e. ES6+) code into backward-compatible JavaScript. Hence, it can run into the older environments such as legacy browsers.

JSX

const add = (a, b) => a + b;

Above babel transform into something like below:

JSX

var add = function (a, b) {
  return a + b;
};

Why other options are incorrect?

8. What is purpose of keys in React lists?

Correct Answer:

Explanation:

keys are special attributes used during rendering lists. It mainly helps React to efficiently update and re-render components by identifying which list item has been added, removed, or changed.

Using key React compares previous and new versions of a list effectively. Key helps in match elements correctly between renders and avoids unnecessary re-renders to improve overall performance.

JSX

const items = ["Apple", "Banana", "Cherry"];

const list = items.map((item, index) => (
  <li key={index}>{item}</li>
));

``

Here, each list item has a unique key to help React for tracking it across renders.

Why other options are incorrect?

9. Which method is used to initialize state in React component?

Correct Answer:

Explanation:

In class-based components state is initialized inside constructor() method. It is a traditional way of setting up state before component mounts.

useState() would be the correct answer, if question is only about functional components.

JSX - Class Component

class MyComponent extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

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

}

Why other options are incorrect?

10. What is everything in React?

Correct Answer:

Explanation:

Everything in React is a component. Whole user interface is built by combining small reusable components. Component represents a part of the UI. It can be functional or class-based. It can accept props and can manage state. It encourages re-usability. It provides modular design.

JSX

function Welcome() {
  return <h1>Hello, React!</h1>;
}

Here, Welcome is a component. In React, whole app itself is a component.

Why other options are incorrect?

11. What is JSX in React?

Correct Answer:

Explanation:

JSX stands for JavaScript XML. It is a syntax extension for JavaScript. Using it developers can write HTML like code directly inside JavaScript, which React then transforms into regular JavaScript function calls. It makes React code easier to read and more expressive with closer to how UI is structured visually.

JSX

const element = <h1>Hello, world!</h1>;

Above JSX code is compiled by tools like below Babel:

JavaScript

React.createElement("h1", null, "Hello, world!");

Why other options are incorrect?

12. Which function is called to render HTML to web page in React?

Correct Answer:

Explanation:

In React, ReactDOM.render() function is used to render or mount component to actual web page i.e. DOM.

It takes two parameters. React element or component and DOM container where it should be rendered.

JSX

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Here, function it connects React to real browser DOM and making UI appear on webpage.

Why other options are incorrect?

13. What is default port where webpack server runs?

Correct Answer:

Explanation:

webpack-dev-server also known as webpack server runs on port 8080 by default. Different port can be explicitly configured in webpack configuration file or via command line to change 8080 default port.

Shell

npx webpack serve

Above command typically starts development server at 8080:

http://localhost:8080

Why other options are incorrect?

14. How to conditionally render components in React?

Correct Answer:

Explanation:

Conditional rendering means showing or hiding components based on a condition. JSX is an expression-based syntax. Ternary conditional operator is most commonly used and it appropriate option for conditional rendering inside JSX in React. It works cleanly inside JSX. It is widely used in real-world React applications.

JSX

function Greeting({ isLoggedIn }) {
  return (
    <div>
                     {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in</h1>}
              </div>
  );
}

Why other options are incorrect?

15. Which command is used to install create-react-app globally?

Correct Answer:

Explanation:

“npm install -g create-react-app” command is used to install “create-react-app” globally via npm.

Shell

npm install -g create-react-app

Here, npm install will install a package and -g will install it globally.

Why other options are incorrect?

Summary

Here, for each and every question; questions are given with multiple option and then correct answer is mentioned. Explanation is described with theory concept as well as code example. Lastly, also mentioned why the other options are in-correct or in-appropriate.

Now, I believe one will be able to properly answer or understand the most popular React Skill interview questions. These questions will be useful to both beginner or intermediate React developers.