React 2 Flashcards

1
Q

: How does the virtual DOM work in React, and why is it important?

A

The virtual DOM is an in-memory representation of the real DOM. React uses it to optimize updates by comparing the virtual DOM with the real DOM (reconciliation) and only applying the necessary changes. This improves performance and makes updates more efficient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are React Hooks, and how do they differ from class lifecycle methods?

A

React Hooks are functions that let you use state and other React features in functional components. They differ from class lifecycle methods by enabling state management and side effects in functional components without using classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the concept of Higher-Order Components (HOCs) and provide use cases.

A

HOCs are functions that take a component and return a new component with additional props or behavior. They are used for code reuse, such as adding authentication checks or handling data fetching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Context API, and how does it help in managing global state?

A

The Context API provides a way to share values between components without passing props manually through every level of the tree. It helps in managing global state by allowing components to access and update shared data.`

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does React’s reconciliation algorithm work?

A

React’s reconciliation algorithm efficiently updates the real DOM by comparing the virtual DOM with the previous version and applying only the necessary changes. It uses heuristics to minimize the number of updates and improve performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe the concept of “lifting state up” in React and provide an example.

A

Lifting state up” involves moving state from child components to a common parent component to share state and behavior between siblings. For example, moving form input state to a parent component so that multiple input fields can be controlled together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the useReducer hook, and how does it compare to useState?

A

The useReducer hook manages complex state logic with a reducer function, similar to how Redux works. It is useful when state transitions are complex, while useState is simpler and better for less complex state management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you optimize the performance of a React application?

A

Performance can be optimized by using techniques such as memoization with React.memo, useMemo, and useCallback, lazy loading components, code splitting, and avoiding unnecessary re-renders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the role of keys in React lists and why they are important.

A

Keys help React identify which items have changed, are added, or are removed from a list. They improve the efficiency of re-rendering by allowing React to optimize updates and reduce unnecessary re-rendering of list items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are React Portals, and when should they be used?

A

eact Portals allow rendering components outside their parent component’s DOM hierarchy. They are useful for modals, tooltips, and other components that need to be visually separate from their parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the benefits and limitations of server-side rendering (SSR) with Next.js.

A

SSR with Next.js improves performance by rendering pages on the server before sending them to the client, which can enhance SEO and load times. Limitations include increased server load and complexity in managing server-side and client-side code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you implement code splitting in a React application?

A

Code splitting can be implemented using dynamic import() statements and React’s lazy and Suspense components to load components asynchronously, reducing the initial bundle size and improving load times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are custom hooks, and how can they help in reusing logic across components?

A

Custom hooks are JavaScript functions that use React hooks internally and can be shared across components to encapsulate and reuse stateful logic or side effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the concept of controlled and uncontrolled components in form handling.

A

Controlled components have their form data managed by React state, while uncontrolled components manage their own state internally and can be accessed using refs. Controlled components provide more control over form inputs and validation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you manage side effects in a React application?

A

Side effects can be managed using the useEffect hook, which allows you to perform operations like data fetching, subscriptions, and manually modifying the DOM in a functional component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Discuss the trade-offs between using Redux and the Context API for state management.

A

Redux is a powerful state management library with a predictable state container and middleware support, suitable for large applications. The Context API is simpler and built-in, ideal for smaller or less complex state management needs.

17
Q

What are fragments in React, and when should they be used?

A

Fragments are used to group multiple elements without adding extra nodes to the DOM. They help in avoiding unnecessary wrapper elements and keeping the DOM structure clean.

18
Q

How does React handle events differently from vanilla JavaScript?

A

React uses a synthetic event system that wraps native events to ensure consistency across different browsers. React’s event handling is also optimized for performance and uses event delegation.

19
Q

Describe the use case and implementation of suspense and lazy loading in React.

A

Suspense and lazy loading enable loading components or data asynchronously, improving performance by reducing the initial load time. React.lazy is used for lazy loading components, and Suspense handles the loading state.

20
Q

How can you use React.memo to optimize component rendering?

A

React.memo is a higher-order component that memoizes a functional component, preventing re-renders if its props haven’t changed. It improves performance by avoiding unnecessary re-rendering of unchanged components.

21
Q

What are the common pitfalls of using useEffect, and how can they be avoided?

A

Common pitfalls include missing dependencies in the dependency array, causing stale or unintended side effects. To avoid issues, always include all dependencies in the array and use the cleanup function to handle side effects properly.

22
Q

How do you handle errors in React components, and what are error boundaries?

A

Errors in React components can be handled using error boundaries, which are components that catch JavaScript errors in their child component tree and display a fallback UI instead of crashing the entire app.

23
Q

Explain the difference between optimistic and pessimistic updates in React.

A

Optimistic updates immediately reflect changes in the UI before confirming the result from a server, improving responsiveness. Pessimistic updates wait for the server response before updating the UI, ensuring accuracy.

24
Q

What is PropTypes, and how does it contribute to type checking in React?

A

PropTypes is a library that provides runtime type checking for React props. It helps catch bugs by ensuring that components receive the correct type of data and provides clear error messages during development.

25
Q

How can you implement dark mode in a React application?

A

How can you implement dark mode in a React application?

26
Q

Describe the role and benefits of using a CSS-in-JS library with React.

A

CSS-in-JS libraries, like styled-components or emotion, allow you to write CSS directly in JavaScript files. They provide scoped styles, dynamic styling, and easier theming, reducing issues with global styles.

27
Q

What are the differences between useRef and createRef?

A

useRef is a hook used in functional components to persist a value across renders without causing re-renders, while createRef is used in class components to create and manage refs directly.

28
Q

How can you handle data fetching in a React component?

A

Data fetching can be handled using the useEffect hook for side effects, or by using custom hooks to encapsulate fetching logic. Async functions can be used within useEffect to fetch data and update component state.

29
Q

What are the best practices for structuring a React project?

A

Best practices include organizing components by feature, using clear and consistent naming conventions, separating concerns (e.g., components, hooks, utilities), and using state management tools as needed.

30
Q

How do you manage complex animations in React, and which libraries can be used?

A

Complex animations can be managed using libraries like React Spring or