React Hooks are an innovative feature introduced in React 16.8 that lets function components use state management and lifecycle features. With useState, useEffect, useContext, and more, you can build complete React applications without class components.
React Hooks is a feature officially released in React 16.8 in 2019 that allows function components to use state and lifecycle features. Before Hooks, using these features required writing class components. With the introduction of Hooks, it became possible to develop fully featured React applications using only function components.
The basic Hooks include useState (state management), useEffect (side effect handling), and useContext (context usage). useState adds a state variable to a component, and useEffect handles side effects such as data fetching, subscriptions, and DOM manipulation. useContext allows data to be shared across the entire component tree without prop drilling.
Additional Hooks include useReducer (complex state logic), useCallback (function memoization), useMemo (value memoization), useRef (mutable ref object), useLayoutEffect (synchronous DOM updates), and useImperativeHandle (ref customization). React 18 added new Hooks such as useId, useTransition, and useDeferredValue, which support concurrency features.
A custom hook (Custom Hook) is a function that starts with use and lets you extract logic to be reused across multiple components. By creating custom hooks such as useForm, useFetch, and useLocalStorage, you can reduce code duplication and separate concerns. Following the Rules of Hooks (call only at the top level, call only within React functions) ensures predictable and consistent behavior.
π¬ Situation: A situation where you are asked about your experience using Hooks in a technical interview
Interviewer"When should you use a cleanup function in useEffect? Do you have an example from an actual project?"
Candidate"A cleanup function is used for unsubscribing, clearing timers, canceling API requests, and so on. In a recent project, I worked with a WebSocket connection and had an experience where a memory leak occurred because the connection was not closed when the component unmounted. I resolved it by calling socket.disconnect() in the cleanup, and since then I have always applied fetch cancellation using AbortController as well."
Interviewer"Please explain the difference between useCallback and useMemo, and when you should use them."
Candidate"useCallback memoizes a function, while useMemo memoizes a computed value. useCallback is useful for preventing callback functions passed as props to child components from being recreated unnecessarily. useMemo is used to cache the result of an expensive calculation. However, applying them everywhere can actually make performance worse, so I profile with React DevTools and apply them only where there is an actual bottleneck."
Violating the Rules of Hooks: Hooks must always be called at the top level of a component. If you call Hooks inside conditionals, loops, or nested functions, React cannot track state correctly, and unexpected bugs occur. Install eslint-plugin-react-hooks to prevent rule violations.
Missing useEffect dependency array: If you leave the dependency array of useEffect empty or omit required dependencies, stale closure problems occur. Always include every value used inside the effect in the dependency array, and enable the exhaustive-deps lint rule.
Excessive memoization: Applying useCallback and useMemo everywhere can actually increase memory usage and reduce code readability. Apply them selectively only where an actual performance problem has been measured, and do not use them for primitive values or simple calculations.