React Questions

1.What is JSX in React?

JSX stands for JavaScript XML.

It is a syntax extension for JavaScript used in React to write HTML-like code inside JavaScript.

Why Use JSX?

JSX makes it easier to build UI in React by letting you write components using syntax that looks like HTML, but is actually converted to JavaScript behind the scenes.

2.What is component in react

In React, a component is a reusable, self-contained piece of UI — like a function or class — that returns JSX (HTML-like syntax) to describe what should appear on the screen.

🧠 Simple Definition

A component is a function or class that returns React elements (usually written in JSX) to be rendered on the web page.

Feature Purpose
props Input data passed from parent
state (in hooks) Internal data that changes over time
useEffect Lifecycle behavior (e.g., fetch API)
event handlers Handle user interactions

 

3. what is props state hook in react

Props are inputs to a component.

  Used to pass data from parent to child component.

  Read-only – the child component cannot modify props.

2️⃣ State

💡 What it is:

  • State is local data that a component manages itself.
  • State can change over time, usually in response to user actions.
  • State changes cause the component to re-render.

import { useState } from ‘react’;

 

function Counter() {

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

 

return (

<>

<p>Count: {count}</p>

<button onClick={() => setCount(count + 1)}>Increment</button>

</>

);

}

 

Hooks

💡 What it is:

  • Hooks are functions that let you “hook into” React’s features (state, lifecycle, etc.) in functional components.
  • Hooks are used only inside functional components (not class components).
  • Common React Hooks:
Hook Purpose
useState() Add state to a functional component
useEffect() Run side effects (like API calls)
useContext() Use global context values
useRef() Get reference to DOM or a value

 

4. Diff between angular and react

  • Angular: A full-fledged framework (developed by Google). It comes with routing, state management, form handling, HTTP client, etc. → batteries included.
  • React: A UI library (developed by Meta/Facebook). Focuses only on building UI components. For routing, state management, etc., you add third-party libraries (e.g., React Router, Redux).

🔹 2. Language

  • Angular: Uses TypeScript by default (strictly typed).
  • React: Uses JavaScript (with optional TypeScript).

🔹 3. Learning Curve

  • Angular: Steeper → you need to learn concepts like modules, decorators, dependency injection, RxJS (observables).
  • React: Easier to pick up → learn JSX, props, state, hooks. More flexible but requires picking additional libraries for big projects.

🔹 4. Performance

  • Angular: Uses a real DOM (with change detection) but optimized with zone.js and ahead-of-time (AOT) compilation.
  • React: Uses a Virtual DOM that efficiently updates only the changed parts → often faster for frequent UI updates.

🔹 5. Architecture

  • Angular: Opinionated, MVC/MVVM-style architecture. Code is more structured.
  • React: Flexible, unopinionated. You can structure your app however you want.

🔹 6. Ecosystem

  • Angular: Provides almost everything out of the box (routing, forms, HTTP, testing).
  • React: Huge ecosystem, but you need to choose packages for routing, state management, forms, etc.

🔹 7. Use Cases

  • Angular: Good for large-scale enterprise apps (e.g., dashboards, CRMs, ERPs) where strict structure helps big teams.
  • React: Good for dynamic, fast UIs (e.g., single-page apps, social media, e-commerce) where flexibility is needed.

5.what is == and === in react?

In React, == and === are JavaScript operators. React itself doesn’t define them—it simply uses JavaScript expressions.

The important difference is:

Operator Name Compares Type Conversion
== Loose Equality Value ✅ Yes
=== Strict Equality Value + Type ❌ No
  • == (loose equality) compares values after performing type conversion.
  • === (strict equality) compares both value and data type without type conversion.
  • In React and modern JavaScript, === is recommended because it prevents bugs caused by implicit type coercion and makes comparisons more predictable.

6.what is diff betn var, let, constant in react?

Comparison Table

Feature var let const
Scope Function Block Block
Can be redeclared ✅ Yes ❌ No ❌ No
Can be reassigned ✅ Yes ✅ Yes ❌ No
Hoisted ✅ Yes (initialized as undefined) ✅ Yes (Temporal Dead Zone) ✅ Yes (Temporal Dead Zone)
Preferred in React ❌ No ✅ Yes ✅ Most Preferred
  • var is function-scoped, can be redeclared and reassigned, and is hoisted with an initial value of undefined.
  • let is block-scoped, cannot be redeclared in the same scope, but can be reassigned. It is hoisted but remains in the Temporal Dead Zone until declared.
  • const is also block-scoped, cannot be redeclared or reassigned, and must be initialized when declared. While a const object’s properties can be modified, the variable itself cannot reference a different object.
  • In React, const is the preferred choice, let is used only when reassignment is necessary, and var is generally avoided.

Which should you use in React?

  • const → Default choice. Use it for variables, functions, imports, objects, arrays, and components that don’t need reassignment.
  • let → Use only when the variable needs to be reassigned.
  • var → Avoid in modern React development because its function scope and hoisting behavior can lead to bugs.

7. what is virtual DOM?

Virtual DOM is a lightweight, in-memory JavaScript representation of the real DOM. React updates the Virtual DOM first, compares it with the previous Virtual DOM using a diffing algorithm, and then updates only the changed parts of the real DOM. This makes UI updates faster and more efficient.

Why do we need Virtual DOM?

Updating the Real DOM is expensive because every change may trigger:

  • Repainting
  • Reflow (layout recalculation)
  • Rendering

For large applications, frequent DOM updates can slow down the UI.

React solves this by using the Virtual DOM.


Real DOM vs Virtual DOM

Real DOM Virtual DOM
Actual HTML elements in the browser JavaScript object representation of the DOM
Slow to update Fast to update
Updates the entire affected DOM Updates only changed elements
Browser performs layout calculations React performs comparisons in memory
More expensive Less expensive

How Virtual DOM Works

Suppose your UI is:

<h1>Hello</h1>
React creates a Virtual DOM object:
{
  type: "h1",
  props: {
    children: "Hello"
  }
}

Advantages of Virtual DOM

  • Faster UI updates
  • Better performance
  • Reduces unnecessary DOM manipulations
  • Efficient rendering
  • Provides a smoother user experience
  • Simplifies building complex user interfaces

8. what is advantages of typescript?

TypeScript is a superset of JavaScript developed by Microsoft. It adds static typing, interfaces, generics, and other features to make JavaScript applications more reliable and maintainable.

TypeScript is a statically typed superset of JavaScript. Its main advantages are compile-time type checking, improved code quality, better IDE support, easier refactoring, interfaces, generics, and enhanced maintainability. It helps catch errors early, improves developer productivity with features like IntelliSense, and is well suited for large-scale applications and team development. That’s why frameworks like Angular are built on TypeScript.

JavaScript vs TypeScript

JavaScript TypeScript
Dynamically typed Statically typed
Errors found at runtime Many errors found at compile time
No interfaces Supports interfaces
No generics Supports generics
Less IDE support Excellent IDE support
Better for small scripts Better for large applications

Leave a Reply

Your email address will not be published. Required fields are marked *