Get ready for your next interview with our comprehensive question library
The Virtual DOM is a JavaScript representation of the actual DOM (Document Object Model). It's a programming concept where a "virtual" representation of the UI is kept in memory and synced with the "real" DOM.
How it works:
This process is called reconciliation and makes React applications faster because manipulating the virtual DOM is much faster than manipulating the real DOM.
JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. It makes React components more readable and easier to write.
JSX gets transpiled to React.createElement()
calls by tools like Babel:
// JSX
const element = <h1>Hello, World!</h1>;
// Transpiled to
const element = React.createElement('h1', null, 'Hello, World!');
Benefits of JSX:
React.createElement()
React Element: A plain JavaScript object that describes what should appear on screen. It's the smallest building block of React apps.
const element = <h1>Hello, World!</h1>;
React Component: A function or class that returns React elements. Components are reusable and can accept inputs (props).
// Function component
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
// Class component
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Functional Components:
function MyComponent(props) {
const [count, setCount] = useState(0);
return <div>{count}</div>;
}
Class Components:
this.state
and lifecycle methodsthis
contextclass MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <div>{this.state.count}</div>;
}
}
There are two main ways to create React components:
1. Function Component (Recommended):
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Or using arrow function
const Welcome = (props) => {
return <h1>Hello, {props.name}!</h1>;
};
2. Class Component:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
Key JSX rules:
className
instead of class
, onClick
instead of onclick
/>
{variable}
or {expression}
disabled={true}
or just disabled
// Correct JSX
function MyComponent() {
return (
<div className="container">
<img src="image.jpg" alt="Description" />
<p>Count: {count}</p>
</div>
);
}
React Fragment lets you group multiple elements without adding an extra node to the DOM. It's useful when you need to return multiple elements from a component but don't want to wrap them in a div.
// Using React.Fragment
function MyComponent() {
return (
<React.Fragment>
<h1>Title</h1>
<p>Description</p>
</React.Fragment>
);
}
// Using short syntax
function MyComponent() {
return (
<>
<h1>Title</h1>
<p>Description</p>
</>
);
}
There are several ways to conditionally render elements:
1. Ternary Operator:
function Greeting({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please log in.</h1>}
</div>
);
}
2. Logical AND (&&):
function Notification({ hasMessages }) {
return (
<div>
{hasMessages && <p>You have new messages!</p>}
</div>
);
}
3. If-else statements:
function UserStatus({ user }) {
if (user.isAdmin) {
return <AdminPanel />;
} else if (user.isLoggedIn) {
return <UserPanel />;
} else {
return <LoginForm />;
}
}
Use the map()
method to render lists, and always provide a unique key
prop:
function TodoList({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={todo.id}>
{todo.text}
</li>
))}
</ul>
);
}
The key
prop helps React identify which items have changed, been added, or removed, improving performance during re-renders.
Props (short for properties) are read-only inputs passed from parent components to child components. They allow data to flow down the component tree.
// Parent component
function App() {
return <Welcome name="John" age={25} />;
}
// Child component
function Welcome(props) {
return <h1>Hello, {props.name}! You are {props.age} years old.</h1>;
}
Props characteristics:
State is a built-in React object used to contain data that may change over the lifetime of a component. When state changes, the component re-renders.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
State characteristics:
Props | State |
---|---|
Read-only | Mutable |
Passed from parent | Local to component |
Cannot be changed by component | Can be changed by component |
External data | Internal data |
Functional parameters | Component memory |
React uses SyntheticEvents, which are wrappers around native events that provide consistent behavior across browsers:
function Button() {
const handleClick = (event) => {
event.preventDefault();
console.log('Button clicked!');
console.log('Event type:', event.type);
};
return (
<button onClick={handleClick}>
Click me
</button>
);
}
Use the preventDefault()
method on the event object:
function Form() {
const handleSubmit = (event) => {
event.preventDefault(); // Prevents form from submitting
console.log('Form submitted');
};
return (
<form onSubmit={handleSubmit}>
<input type="text" />
<button type="submit">Submit</button>
</form>
);
}
There are several ways to pass parameters:
1. Arrow function in JSX:
function ItemList({ items }) {
const handleClick = (id) => {
console.log('Clicked item:', id);
};
return (
<div>
{items.map(item => (
<button key={item.id} onClick={() => handleClick(item.id)}>
{item.name}
</button>
))}
</div>
);
}
2. Bind method:
function ItemList({ items }) {
const handleClick = (id, event) => {
console.log('Clicked item:', id);
};
return (
<div>
{items.map(item => (
<button key={item.id} onClick={handleClick.bind(null, item.id)}>
{item.name}
</button>
))}
</div>
);
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime