2. What are React hooks, and why would you use useEffect over lifecycle methods in class components?
Anónimo
React Hooks are functions that let you "hook into" React state and lifecycle features from functional components. The useEffect Hook replaces multiple lifecycle methods (componentDidMount, componentDidUpdate, and componentWillUnmount) with a single, unified API. Unlike class methods, useEffect allows you to group related logic together rather than splitting it across different methods. For example, if you need to fetch data and set up a subscription, you can handle both setup and cleanup in one useEffect block. Additionally, hooks reduce boilerplate, make code more reusable (via custom hooks), and avoid the complexity of this binding in classes. However, you must manage dependencies carefully in the dependency array to prevent infinite loops or stale closures.