Interview Questions For π π«π¨π§πππ§π πππ―ππ₯π¨π©ππ« (πππππ ππ) π based on several interview compilation by Asim MahmoodAsim Mahmood
function reverseString(str) {
let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
console.log(reverseString("React")); // Output: tcaeRA generator function is a special type of function that can pause and resume execution. It is defined using function* syntax and produces an iterator.
How It Works:
- Use
yieldto pause and return a value. - Resume execution using the iterator's
next()method.
function* generatorExample() {
yield "First";
yield "Second";
yield "Third";
}
const gen = generatorExample();
console.log(gen.next().value); // "First"
console.log(gen.next().value); // "Second"
console.log(gen.next().value); // "Third"A closure is a function that "remembers" the variables from its lexical scope, even after the outer function has executed.
function outerFunction(outerVar) {
return function innerFunction(innerVar) {
return `Outer: ${outerVar}, Inner: ${innerVar}`;
};
}
const closureFunc = outerFunction("React");
console.log(closureFunc("JS")); // Output: "Outer: React, Inner: JS"A higher-order component (HOC) is a function that takes a component and returns a new component with added functionality.
const withExtraProps = (WrappedComponent) => (props) => {
return <WrappedComponent extraProp="Hello" {...props} />;
};- State: Managed within the component, mutable, used for dynamic rendering.
- Props: Passed from parent to child, immutable, used to pass data.
A callback function is a function passed as an argument to another function and is executed later.
function greet(callback) {
console.log("Hello");
callback();
}
greet(() => console.log("World")); // Output: Hello WorldThe spread operator (...) expands arrays or objects.
const arr = [1, 2, 3];
const newArr = [...arr, 4];
console.log(newArr); // [1, 2, 3, 4]- Shallow Copy: Copies only top-level properties.
const obj = { a: 1, b: { c: 2 } }; const shallowCopy = { ...obj }; shallowCopy.b.c = 3; console.log(obj.b.c); // 3
- Deep Copy: Copies all levels.
const deepCopy = JSON.parse(JSON.stringify(obj));
The Virtual DOM is a lightweight representation of the real DOM. React uses it to optimize rendering by updating only changed elements.
Hooks allow using state and lifecycle methods in functional components.
- Common Hooks:
useState,useEffect,useMemo,useCallback,useContext.
useMemo: Memoizes the value of a computation.React.memo: Prevents re-rendering of a component unless props change.
- useCallback:
const memoizedCallback = useCallback(() => console.log("Callback"), []);
- useMemo:
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
function Timer() {
const [seconds, setSeconds] = useState(0);
const [timer, setTimer] = useState(null);
const start = () => setTimer(setInterval(() => setSeconds((s) => s + 1), 1000));
const stop = () => clearInterval(timer);
return (
<>
<div>{seconds}s</div>
<button onClick={start}>Start</button>
<button onClick={stop}>Stop</button>
</>
);
}useEffect(() => {
console.log("Component Mounted");
return () => console.log("Component Unmounted");
}, []);Use a callback function.
function Parent() {
const handleData = (data) => console.log(data);
return <Child sendData={handleData} />;
}
function Child({ sendData }) {
return <button onClick={() => sendData("Hello Parent!")}>Send</button>;
}React relies on immutability for efficient re-renders.
Redux provides a predictable state container for managing global state in large applications.
Middlewares allow side effects, like API calls or logging, during the Redux action lifecycle.
- Controlled: Value is controlled via state.
- Uncontrolled: Value managed by DOM.
A reducer is a pure function returning new state based on the current state and action.
const reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
default:
return state;
}
};Use React.memo or useMemo.
Provides a way to share values between components without prop-drilling.
- Context API
- Props
- State Management Libraries (Redux)
- Memoization
- Lazy Loading
- Code Splitting
- Debouncing
const LazyComponent = React.lazy(() => import("./LazyComponent"));Use React.memo, useCallback, or useMemo.
import { render, screen } from "@testing-library/react";
import MyComponent from "./MyComponent";
test("renders the component", () => {
render(<MyComponent />);
expect(screen.getByText("Hello")).toBeInTheDocument();
});const isEmpty = (obj) => Object.keys(obj).length === 0;Bootstrap simplifies responsive design. It provides prebuilt classes for layouts, grids, and components like buttons and modals.
the questions