1.React DOM
- What is React DOM?
React DOM is the bridge between your React components and the actual browser DOM (Document Object Model). It allows your React code (written in JSX and JavaScript) to be displayed in the browser.
- Why do we use it?
React uses a virtual DOM for better performance. Instead of updating the entire web page every time something changes, React compares the new virtual DOM with the previous one and updates only the changed elements in the real DOM using React DOM. This process is called reconciliation and makes React fast and efficient.
- When is React DOM used?
React DOM is used when you want to render your React components into the browser. Usually, this happens in your index.js file, where the main component (like ) is mounted to a real HTML element.
- Where is it used?
It is used in web projects. For React Native (for mobile apps), React DOM is replaced with React Native modules.
How is it used?
Here’s a basic example:
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
In React 18+, the syntax slightly changes:
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
2. SPA vs MPA
- What are SPA and MPA?
SPA (Single Page Application) loads one HTML file and updates the content dynamically using JavaScript without refreshing the page.
MPA (Multi Page Application) loads a new HTML page from the server each time the user navigates to a new route.
- Why are they important?
SPAs offer a fast, seamless user experience by reducing load time and transitions. On the other hand, MPAs are better for SEO (Search Engine Optimization) and are often used for large websites like e-commerce or blogs.
- When should you use them?
Use SPA for dynamic web apps like dashboards, social platforms, or productivity tools. Use MPA for static websites or sites where SEO is critical.
- Where are they used?
SPAs: React, Angular, Vue.
MPAs: Traditional HTML, PHP, JSP, and frameworks like Spring MVC or Laravel.
3. npm vs npx
- What is npm and npx?
npm (Node Package Manager) is a tool to install, manage, and share JavaScript packages.
npx (Node Package Executer) is a tool to run npm packages without installing them permanently.
- Why are they important?
With npm, you can install libraries like React, Axios, or Lodash. npx helps when you want to run tools (like create-react-app
) just once, without keeping them in your system.
- When are they used?
Use npm when you want a package available in your project repeatedly. Use npx when you want to run a package only once, like setting up a new React project.
- Where are they used? They are used in your command line or terminal inside the project directory.
How are they used?
To install Axios using npm:
npm install axios
To create a React app using npx:
npx create-react-app my-app
4. JSX (JavaScript XML)
- What is JSX?
JSX is a syntax extension that lets you write HTML-like code inside JavaScript. It makes your React components more readable and intuitive.
- Why do we use JSX?
JSX is easier to understand and write compared to plain JavaScript. Instead of calling functions like React.createElement()
, JSX allows you to visually structure UI like HTML.
- When should you use JSX?
Whenever you want to build UI components in React. JSX is used inside functional or class components.
- Where is it used?
Inside .js
or .jsx
files in your React app.
- How does JSX work?
JSX gets compiled into JavaScript behind the scenes. This:
const element = <h1>Hello, world!</h1>;
Turns into:
const element = React.createElement('h1', null, 'Hello, world!');
5. Library vs Framework
- What is the difference?
A library is a collection of functions and tools you can use in your code (like React).
A framework provides a complete structure and decides how your app is built and runs (like Angular).
- Why does it matter?
Libraries offer flexibility. Frameworks offer structure. Depending on your needs, you may prefer one over the other.
- When to choose which?
Use a library like React if you want control and customization. Use a framework like Angular if you want a pre-defined way to build applications.
- Where are they used?
React (library) is used in lightweight front-end apps. Angular (framework) is used in enterprise-grade apps needing built-in tools.
6. Conditional Rendering in React
- What is it?
Conditional rendering means showing or hiding UI elements based on certain conditions.
- Why is it needed?
Your app often needs to show different content for different users or states — like showing a login form if the user isn’t logged in.
- When do we use it?
Whenever you need to change the UI based on data, props, or user interaction.
- Where is it written? Inside JSX, often within a return statement of a component.
7. How Hooks Work in React
- What are Hooks?
Hooks are built-in React functions that let you use state and lifecycle features inside functional components.
- Why were hooks introduced?
Before hooks, only class components could use features like state and lifecycle methods. Hooks allow you to write simpler and reusable functional components.
- When are they used?
Use useState() when you need local component state. Use useEffect() when you want to run side effects like API calls.
- Where are hooks used?
Only inside functional components — not in regular functions or class components.
Top comments (0)