Learn React Basics – Interview Q&A

51

What is React?

React is an open-source JavaScript library for building user interfaces, especially single-page applications.

Created by Facebook, it allows developers to create reusable UI components.

52

What are the main features of React?

  • Component-based architecture
  • Virtual DOM
  • One-way data flow
  • JSX syntax
  • Declarative UI
  • Reusability
  • Strong community & ecosystem
53

What is JSX?

JSX stands for JavaScript XML.

It is a syntax extension that allows you to write HTML-like code inside JavaScript.

Browser doesn’t understand JSX → Babel transpiles it to React.createElement() calls.

54

What is the difference between Element and Component in React?

  • Element: Plain object describing what to render (like <div>)
  • Component: Function or class that returns elements (reusable piece of UI)

Example:

// Element
const title = <h1>Hello</h1>;

// Component
function Title() {
  return <h1>Hello</h1>;
}
55

How do you create a React app?

Most common ways (2025):

  1. npx create-react-app my-app (classic)
  2. npx create-vite@latest my-app -- --template react (faster & modern)
  3. npx create-next-app@latest (with Next.js)
56

What is a component in React?

A component is an independent, reusable piece of UI.

Two types:

  • Function component (preferred today)
  • Class component (older style)
57

Write a simple functional component.

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

// or arrow function (very common)
const Welcome = ({ name }) => <h1>Hello, {name}</h1>;
58

What are props in React?

Props (properties) are read-only data passed from parent to child component.

Like function arguments.

<Welcome name="Sara" />
59

What is state in React?

State is a built-in object that holds data that can change over time.

When state changes → component re-renders.

In functional components we use useState hook.

60

Difference between props and state?

Props State
Mutable? No (read-only) Yes
Who owns? Parent component Component itself
Can be passed? Yes, to children No (but can pass via props)
61

What is the useState hook? Give example.

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>+1</button>
    </>
  );
}
62

Why do we need to use setState / setCount instead of modifying state directly?

Direct mutation doesn’t trigger re-render.

React compares previous & new state → only re-renders when using setter function.

63

What is an event handler in React?

Functions that respond to user actions (click, change, submit, etc.).

Use camelCase: onClick, onChange, onSubmit

64

How do you pass data from child to parent?

Pass a callback function as prop from parent to child.

// Parent
function Parent() {
  const [name, setName] = useState("");
  return <Child setName={setName} />;
}

// Child
function Child({ setName }) {
  return <input onChange={e => setName(e.target.value)} />;
}
65

What is conditional rendering in React?

  • Ternary operator: {isLoggedIn ? <Dashboard /> : <Login />}
  • Logical &&: {isLoading && <Spinner />}
  • Early return: if (!user) return <Login />;
66

How do you render a list in React?

const users = ["Alice", "Bob", "Charlie"];

return (
  <ul>
    {users.map((user, index) => (
      <li key={index}>{user}</li>
    ))}
  </ul>
);
67

Why do we need keys in lists?

Keys help React identify which items have changed, added, or removed.

Without keys → unnecessary re-renders or lost state.

Best: use unique ID from data, not array index.

68

What is React.Fragment or <></>?

Allows grouping multiple elements without adding extra DOM node.

return (
  <>
    <h1>Title</h1>
    <p>Content</p>
  </>
);
69

What are class components? (basic understanding)

Older way to write components using ES6 classes.

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

Today mostly replaced by functional components + hooks.

70

What is the purpose of render() method? (class components)

It is the only required method in class component.

Returns the JSX (or null) that describes what should appear on screen.

71

What are synthetic events in React?

React wraps native browser events into cross-browser compatible synthetic events.

Ensures consistent behavior across browsers.

72

How do you prevent default behavior in React events?

<form onSubmit={(e) => {
  e.preventDefault();
  // handle form
}}>
73

What is one-way data binding / unidirectional data flow?

Data flows only from parent → child via props.

Children cannot directly modify parent state → must use callback functions.

Makes debugging easier and predictable.

74

What file is the entry point of a React application?

index.js or main.jsx

import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
75

What is the purpose of ReactDOM?

It is the bridge between React and the browser DOM.

Methods like createRoot, render, hydrateRoot.

76

Can we return multiple elements from a component without wrapper?

Yes — using <></> (Fragment) or React.Fragment.

77

What happens if we don’t give key in list items?

React shows warning in console.

May cause performance issues, wrong UI updates, or lost component state.

78

How do you update state based on previous state?

setCount(prev => prev + 1);

Safer when multiple updates happen in same event loop.

79

What is the children prop?

Special prop that contains everything passed between opening & closing tags.

<Card>
  <h2>Title</h2>
  Content here
</Card>
80

How do you pass multiple props to a component?

<User
  name="John"
  age={28}
  isActive={true}
  address={{ city: "Mumbai", pin: 400001 }}
/>
81

What will happen if we call setState in constructor?

It’s allowed but not recommended.

Better to use state initializer:

state = { count: 0 };
82

How do you style React components? (basic ways)

  • Inline styles: style={{ color: 'red' }}
  • CSS class: className="btn"
  • CSS Modules
  • Styled-components / Emotion
  • Tailwind CSS
83

What is the difference between className and class in JSX?

class is a reserved word in JavaScript → use className instead.

84

How do you handle input changes in React?

const [value, setValue] = useState("");

<input
  value={value}
  onChange={(e) => setValue(e.target.value)}
/>
85

What are controlled components?

Form elements whose value is controlled by React state.

Most common and recommended way.

86

What are uncontrolled components?

Form elements that maintain their own state in the DOM.

Use defaultValue and refs to access value.

Less common today.

87

How do you create a ref in functional component?

const inputRef = useRef(null);

<input ref={inputRef} />

// later
inputRef.current.focus();
88

What is lifting state up?

Moving shared state to the closest common parent component.

Allows sibling components to share and sync data.

89

What is prop drilling?

Passing props through many levels even when intermediate components don’t use them.

Solution: Context API, Zustand, etc.

90

Can we use async/await inside useState initializer?

No — initializer must be synchronous.

Use useEffect for async operations.

91

What does { …props } mean?

Spread operator — passes all remaining props to child.

function Button({ children, ...rest }) {
  return <button {...rest}>{children}</button>;
}
92

How do you destructure props?

function User({ name, age, city }) {
  return <p>{name} is {age} from {city}</p>;
}
93

What happens when you return null from a component?

Nothing is rendered (component is skipped).

Useful for conditional rendering.

94

Can we call hooks conditionally?

No — violates Rules of Hooks.

Hooks must be called in the same order on every render.

95

What is the purpose of key prop when not in a list?

Forcing remount when key changes.

{tab === 'home' ? <Home key="home" /> : <Profile key="profile" />}
96

How do you comment in JSX?

{/* This is a comment */}

Inside curly braces because it’s JavaScript.

97

What is createElement?

Low-level method that JSX compiles to.

React.createElement("div", { className: "box" }, "Hello")
98

Can we use React without JSX?

Yes — using React.createElement directly.

Less readable, so JSX is preferred.

99

What is the React strict mode?

<React.StrictMode> enables extra checks in development:

  • Double rendering of components (detects side effects)
  • Warnings for deprecated features
100

Should we keep all logic inside component or separate it?

Best practice: extract reusable logic into custom hooks or utility functions.

Keeps components clean and focused on UI.