React Flashcards
(29 cards)
What is a React component?
A React component is a reusable function or class that returns a UI element, typically as JSX, to render content.
What is the difference between a functional and class component?
Functional components are JavaScript functions using hooks for state; class components are ES6 classes with this.state
and lifecycle methods.
What is JSX in React?
JSX is a syntax extension for JavaScript that looks like HTML and allows embedding UI markup in React code.
How does the virtual DOM work in React?
The virtual DOM is an in-memory representation of the real DOM. React compares it with the previous version to compute minimal updates.
What is the purpose of useState
hook?
useState
manages state in functional components, returning a state value and an updater function.
What is the difference between useState
and useReducer
?
useState
is for simple state updates; useReducer
manages complex state logic with a reducer function.
Explain the useEffect
hook.
useEffect
handles side effects (e.g., API calls, subscriptions) after render.
What is a controlled component in React?
A controlled component has its form input value controlled by React state, updated via onChange
.
What is the purpose of key
prop in React?
The key
prop uniquely identifies elements in a list, helping React optimize rendering by tracking changes.
How does React’s reconciliation process work?
Reconciliation compares the new virtual DOM with the previous one to determine minimal DOM updates.
What is the useContext
hook, and when is it used?
useContext
accesses a React context to share data across components without prop drilling.
Explain the purpose of React.memo
.
React.memo
prevents unnecessary re-renders of functional components by memoizing them.
What are React hooks, and why were they introduced?
Hooks are functions (e.g., useState
, useEffect
) that add state and lifecycle features to functional components.
How does useCallback
improve performance?
useCallback
memoizes a callback function, preventing it from being recreated on re-renders unless dependencies change.
What is the difference between useMemo
and useCallback
?
useMemo
memoizes a computed value; useCallback
memoizes a function.
What is a custom hook in React?
A custom hook is a reusable function that encapsulates hook-based logic.
Explain the role of React.Fragment
.
React.Fragment
groups multiple elements without adding extra DOM nodes.
How does error boundary work in React?
An error boundary is a class component that catches JavaScript errors in its child tree.
What is lazy loading in React, and how is it implemented?
Lazy loading defers loading of components until needed, using React.lazy
and Suspense
.
How do you optimize a React application’s performance?
Use React.memo
, useCallback
, useMemo
, lazy loading, code splitting, and avoid unnecessary renders.
What’s the real purpose of useEffect and how do you avoid infinite loops with it?
Purpose: The useEffect hook in React is used to handle side effects in functional components, such as data fetching, subscriptions, or manually changing the DOM. It runs after the render is committed to the screen. Avoiding Infinite Loops: Infinite loops occur when useEffect re-renders the component continuously due to state or prop changes. To avoid this, specify a dependency array with only the variables the effect depends on. Use an empty array ([]) to run it only once on mount and cleanup on unmount.
How is useCallback different from useMemo?
useCallback: Returns a memoized version of a callback function that only changes if its dependencies change. Useful for optimizing child components relying on reference equality. useMemo: Returns a memoized value that only recomputes when dependencies change. Used for caching expensive computations. Key Difference: useCallback memoizes functions, useMemo memoizes values.
What are Higher Order Components (HOC)?
Higher Order Components (HOC) are a pattern in React where a function takes a component and returns a new component with additional props or behavior. It’s used for code reuse, logic abstraction, and enhancing components (e.g., with data or authentication). Example: withAuthentication(User) returns a new component that checks auth before rendering User.
How do you optimize a React app’s performance – in real-world production code?
Optimization techniques include: using React.memo() to prevent unnecessary re-renders, implementing useCallback and useMemo for performance-critical functions and values, code-splitting with lazy() and Suspense, using production builds (minification, tree-shaking), optimizing images and assets, and profiling with React Developer Tools to identify bottlenecks.