React Flashcards

1
Q

What is the purpose of React’s useEffect hook, and how does dependency array management affect performance and behavior?

A

useEffect is used for handling side effects in functional components (e.g., data fetching, subscriptions, timers).
The dependency array controls when the effect runs:
Empty array []: Effect runs once on mount.
No array: Effect runs on every render.
With dependencies: Effect runs only when the dependencies change.
Performance impact: Proper dependency management prevents unnecessary re-renders and side effects, ensuring efficient updates.

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

How would you implement a custom hook to fetch data from an API and handle loading, error, and success states?

A

import { useState, useEffect } from “react”;

function useFetch(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
setLoading(true);
fetch(url)
.then((response) => response.json())
.then((data) => {
setData(data);
setLoading(false);
})
.catch((error) => {
setError(error);
setLoading(false);
});
}, [url]);

return { data, loading, error };
}
This hook fetches data and manages loading and error states, providing a clean way to reuse the logic across components.

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

Can you explain the concept of “reconciliation” in React and how React optimizes re-rendering?

A

Reconciliation is the process React uses to update the DOM efficiently. React uses a virtual DOM, which is an in-memory representation of the real DOM.
When state or props change, React compares the virtual DOM with a previous version using a diffing algorithm and only applies the minimal number of updates to the real DOM to reflect the changes.
This process minimizes costly operations and improves performance, especially in complex UIs.

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

What is the difference between controlled and uncontrolled components in React?

A

Controlled components: The form input’s value is controlled by React state (the source of truth).

function App() {
const [value, setValue] = useState(“”);
return <input value={value} onChange={(e) => setValue(e.target.value)} />;
}
Uncontrolled components: The DOM maintains the input’s state, and refs are used to access values.
function App() {
const inputRef = useRef(null);
return <input ref={inputRef} />;
}

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

How do you use React.memo() to optimize performance, and what are the trade-offs?

A

React.memo() is a higher-order component that memoizes the result of a component’s render, preventing it from re-rendering unless its props change.
const MyComponent = React.memo((props) => {
return <div>{props.value}</div>;
});

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

useState

A

Manages local state in a functional component.
import { useState } from ‘react’;

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

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

useEffect

A

Handles side effects (like data fetching, subscriptions, or manually updating the DOM) in a functional component.

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

useRef

A

Creates a mutable reference that persists across renders. Commonly used for accessing DOM elements or storing mutable values.

import { useRef, useEffect } from ‘react’;

function FocusInput() {
const inputRef = useRef(null);

useEffect(() => {
inputRef.current.focus(); // Focus the input field when the component mounts
}, []);

return <input ref={inputRef} placeholder=”Focus me on render” />;
}

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

useReducer

A

Manages more complex state logic than useState, similar to Redux reducer patterns.

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

useMemo

A

useMemo is a React Hook that lets you cache the result of a calculation between re-renders.

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