The document contains 50 advanced React.js questions and detailed answers covering various topics such as React Hooks, performance optimization, state management, and component architecture. Key concepts discussed include useEffect, reconciliation, controlled vs uncontrolled components, and the Context API. It also addresses performance issues, error handling, and best practices for building reusable components.
The document contains 50 advanced React.js questions and detailed answers covering various topics such as React Hooks, performance optimization, state management, and component architecture. Key concepts discussed include useEffect, reconciliation, controlled vs uncontrolled components, and the Context API. It also addresses performance issues, error handling, and best practices for building reusable components.
React Hooks are functions introduced in React 16.8 that allow you to use state and other React features in functional components without needing a class. Examples include useState for state management and useEffect for side effects.
2. Explain the useEffect hook.
useEffect allows you to perform side effects in function components, such as fetching data, subscribing to events, or modifying the DOM. It runs after rendering and can be configured to run only when dependencies change.
3. What is the difference between useMemo and useCallback?
useMemo is used to memoize the result of a computation, preventing expensive recalculations. useCallback, on the other hand, is used to memoize a function, ensuring it does not get recreated on every render unless dependencies change.
4. What is reconciliation in React?
Reconciliation is the process by which React updates the DOM efficiently. React compares the virtual DOM with the previous version and applies only the necessary changes to the real DOM, making updates faster and smoother.
5. What is the difference between controlled and uncontrolled components?
Controlled components have their state managed by React through props, while uncontrolled components manage their own state using the DOM, typically via refs.
6. What is React Context API?
The Context API provides a way to pass data through the component tree without manually passing props at every level. It is useful for managing global state, such as themes or authentication status.
7. What is prop drilling and how do you avoid it?
Prop drilling occurs when props are passed down through multiple layers of components. This can be avoided using the Context API, Redux, or Zustand for state management.
8. What is the significance of keys in React lists?
Keys help React identify which list items have changed, been added, or removed. They improve performance by allowing React to update only the necessary elements instead of re-rendering the entire list.
9. What are higher-order components (HOC)?
HOCs are functions that take a component and return a new component with additional functionality. They are commonly used for reusing component logic, such as authentication handling or data fetching.
10. What is the difference between useState and useReducer?
useState is a simpler way to manage component-level state, while useReducer is better suited for complex state logic and state transitions, typically used when actions and reducers are involved.
11. How does React Fiber work?
React Fiber is a new reconciliation algorithm introduced in React 16. It allows React to work asynchronously, prioritizing updates and improving rendering performance.
12. What is lazy loading in React?
Lazy loading defers the loading of components until they are needed, reducing the initial bundle size. React.lazy() and Suspense are used for implementing lazy loading.
13. How do you optimize React performance?
Optimizations include using React.memo for memoization, avoiding unnecessary renders, using useCallback and useMemo, and implementing code-splitting.
14. What is a pure component in React?
A PureComponent is a class component that only re-renders when its props or state change, using a shallow comparison to determine changes.
15. What is React Portal?
React Portals allow rendering a child component outside its parent hierarchy while maintaining React's event system.
16. What is the use of forwardRef?
forwardRef allows parent components to pass a ref to a child component, useful for accessing DOM elements or React components.
17. What is the difference between state and props?
State is internal to a component and mutable, while props are external, passed from a parent component, and usually immutable.
18. What are synthetic events in React?
Synthetic events are React's cross-browser wrapper around the native DOM events, ensuring consistency across browsers.
19. What is the role of React.Fragment?
React.Fragment allows grouping multiple elements without adding extra nodes to the DOM.
20. How does React handle forms?
React supports controlled and uncontrolled components for forms. Controlled components store form data in state, while uncontrolled components use refs.
21. What is code splitting?
Code splitting allows loading only necessary JavaScript code when required, improving performance. It can be achieved using React.lazy and dynamic imports.
22. How does React handle errors?
React uses Error Boundaries to catch JavaScript errors in components and display a fallback UI instead of crashing the entire app.
23. What is server-side rendering (SSR)?
SSR generates the initial HTML on the server before sending it to the client, improving SEO and initial load performance.
24. What is hydration in React?
Hydration is React attaching event listeners to server-rendered HTML, making it interactive without reloading the page.
25. What is suspense in React?
Suspense allows components to delay rendering until dependent asynchronous operations are completed.