What are hooks in React?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Hooks in React

Hooks are a powerful feature introduced in React 16.8 that allow developers to use state and other React features in functional components. Before Hooks, state management and lifecycle methods were only possible in class-based components. Hooks have revolutionized how React developers build and manage component logic, promoting cleaner and more reusable code.

1. What Are Hooks?

  • Definition: Hooks are functions that let you "hook into" React state and lifecycle features from functional components.

  • Purpose: They enable functional components to manage state, perform side effects, and leverage other React features without needing to convert them into class components.

2. Why Use Hooks?

Hooks address several limitations and complexities associated with class-based components:

  • Simpler Syntax: Functional components with Hooks are generally easier to read and write compared to class components.

  • Reusability: Hooks allow for the reuse of stateful logic without changing the component hierarchy, promoting better code organization.

  • Avoiding this Binding: Hooks eliminate the need to bind this in class components, reducing boilerplate and potential errors.

  • Enhanced Functionality: Hooks provide access to React features like state and lifecycle methods in a more intuitive and flexible manner.

3. Built-In Hooks in React

React provides several built-in Hooks, each serving a specific purpose. Here's an overview of the most commonly used Hooks:

a. useState

  • Purpose: Adds state management to functional components.

  • Usage:

    import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); // Initializes state return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); } export default Counter;
  • Key Points:

    • Returns a pair: the current state value and a function to update it.
    • Can handle multiple state variables by calling useState multiple times.

b. useEffect

  • Purpose: Performs side effects in functional components, such as data fetching, subscriptions, or manual DOM manipulations.

  • Usage:

    import React, { useState, useEffect } from 'react'; import axios from 'axios'; function DataFetcher() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { axios.get('https://api.example.com/data') .then(response => { setData(response.data); setLoading(false); }) .catch(err => { setError('Error fetching data'); setLoading(false); }); }, []); // Empty dependency array runs once on mount if (loading) return <p>Loading...</p>; if (error) return <p>{error}</p>; return <div>{JSON.stringify(data)}</div>; } export default DataFetcher;
  • Key Points:

    • Runs after every render by default but can be controlled with the dependency array.
    • Can return a cleanup function to avoid memory leaks.

c. useContext

  • Purpose: Accesses the value of a context without needing to wrap components in a Consumer.

  • Usage:

    import React, { useContext } from 'react'; const ThemeContext = React.createContext('light'); function ThemedButton() { const theme = useContext(ThemeContext); // Accesses context value return <button className={theme}>I am styled by theme context!</button>; } function App() { return ( <ThemeContext.Provider value="dark"> <ThemedButton /> </ThemeContext.Provider> ); } export default App;
  • Key Points:

    • Simplifies consuming context values in deeply nested components.
    • Enhances code readability and maintainability.

d. useReducer

  • Purpose: Manages complex state logic in functional components, similar to Redux but localized to the component.

  • Usage:

    import React, { useReducer } from 'react'; const initialState = { count: 0 }; function reducer(state, action) { switch (action.type) { case 'increment': return { count: state.count + 1 }; case 'decrement': return { count: state.count - 1 }; default: throw new Error(); } } function Counter() { const [state, dispatch] = useReducer(reducer, initialState); return ( <div> <p>Count: {state.count}</p> <button onClick={() => dispatch({ type: 'increment' })}>+</button> <button onClick={() => dispatch({ type: 'decrement' })}>-</button> </div> ); } export default Counter;
  • Key Points:

    • Ideal for managing state transitions that depend on previous states.
    • Enhances readability when dealing with multiple related state variables.

e. useCallback

  • Purpose: Memoizes callback functions to prevent unnecessary re-creations on every render.

  • Usage:

    import React, { useState, useCallback } from 'react'; function ExpensiveComponent({ onClick }) { console.log('Rendering ExpensiveComponent'); return <button onClick={onClick}>Click Me</button>; } const MemoizedExpensiveComponent = React.memo(ExpensiveComponent); function App() { const [count, setCount] = useState(0); const handleClick = useCallback(() => { setCount(prevCount => prevCount + 1); }, []); // Dependencies array return ( <div> <p>Count: {count}</p> <MemoizedExpensiveComponent onClick={handleClick} /> </div> ); } export default App;
  • Key Points:

    • Prevents unnecessary re-renders of child components by maintaining the same function reference.
    • Useful when passing callbacks to optimized child components.

f. useMemo

  • Purpose: Memoizes the result of an expensive computation to avoid recalculating on every render.

  • Usage:

    import React, { useState, useMemo } from 'react'; function App() { const [count, setCount] = useState(0); const [text, setText] = useState(''); const expensiveCalculation = useMemo(() => { console.log('Calculating...'); let total = 0; for (let i = 0; i < 1000000; i++) { total += i; } return total; }, [count]); // Recalculates only when 'count' changes return ( <div> <p>Expensive Calculation Result: {expensiveCalculation}</p> <button onClick={() => setCount(count + 1)}>Increment</button> <input value={text} onChange={(e) => setText(e.target.value)} placeholder="Type here" /> </div> ); } export default App;
  • Key Points:

    • Enhances performance by avoiding costly recalculations.
    • Should be used judiciously, only when necessary.

g. useRef

  • Purpose: Provides a mutable ref object that persists across renders without causing re-renders when updated. Commonly used to access DOM elements directly.

  • Usage:

    import React, { useRef } from 'react'; function TextInput() { const inputRef = useRef(null); const focusInput = () => { inputRef.current.focus(); }; return ( <div> <input ref={inputRef} type="text" /> <button onClick={focusInput}>Focus Input</button> </div> ); } export default TextInput;
  • Key Points:

    • Can hold any mutable value, not just DOM references.
    • Useful for storing values that need to persist without triggering re-renders.

h. useLayoutEffect

  • Purpose: Similar to useEffect, but fires synchronously after all DOM mutations. Useful for reading layout from the DOM and synchronously re-rendering.

  • Usage:

    import React, { useState, useLayoutEffect, useRef } from 'react'; function LayoutComponent() { const divRef = useRef(null); const [width, setWidth] = useState(0); useLayoutEffect(() => { setWidth(divRef.current.offsetWidth); }, []); return ( <div ref={divRef}> <p>The width of this div is: {width}px</p> </div> ); } export default LayoutComponent;
  • Key Points:

    • Blocks the browser from painting until the effect is complete.
    • Can lead to performance issues if overused.

i. useImperativeHandle

  • Purpose: Customizes the instance value that is exposed when using ref. Typically used with forwardRef.

  • Usage:

    import React, { useImperativeHandle, useRef, forwardRef } from 'react'; const FancyInput = forwardRef((props, ref) => { const inputRef = useRef(); useImperativeHandle(ref, () => ({ focus: () => { inputRef.current.focus(); }, })); return <input ref={inputRef} />; }); function App() { const inputRef = useRef(); return ( <div> <FancyInput ref={inputRef} /> <button onClick={() => inputRef.current.focus()}>Focus Fancy Input</button> </div> ); } export default App;
  • Key Points:

    • Controls which properties and methods are accessible to parent components.
    • Enhances encapsulation and abstraction.

j. useDebugValue

  • Purpose: Displays a label for custom Hooks in React DevTools, aiding in debugging.

  • Usage:

    import React, { useState, useDebugValue } from 'react'; function useFriendStatus(friendID) { const [isOnline, setIsOnline] = useState(null); useEffect(() => { function handleStatusChange(status) { setIsOnline(status.isOnline); } ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange); return () => { ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange); }; }, [friendID]); useDebugValue(isOnline ? 'Online' : 'Offline'); return isOnline; } function FriendStatus(props) { const isOnline = useFriendStatus(props.friend.id); if (isOnline === null) { return 'Loading...'; } return isOnline ? 'Online' : 'Offline'; }
  • Key Points:

    • Primarily used internally by React and custom Hooks.
    • Enhances the developer experience by providing more context in debugging tools.

4. Rules of Hooks

To ensure Hooks work correctly and predictably, React enforces specific rules:

  1. Only Call Hooks at the Top Level: Do not call Hooks inside loops, conditions, or nested functions. Always use Hooks at the top level of your React function to ensure consistent behavior.

  2. Only Call Hooks from React Functions: Hooks can only be called from functional components or custom Hooks. Do not call Hooks from regular JavaScript functions.

Example of Correct Hook Usage:

import React, { useState, useEffect } from 'react'; function ExampleComponent() { const [count, setCount] = useState(0); useEffect(() => { // Side effect logic }, [count]); return <div>{count}</div>; }

Example of Incorrect Hook Usage:

import React, { useState } from 'react'; function ExampleComponent() { if (someCondition) { const [count, setCount] = useState(0); // ❌ Hooks inside condition } return <div>Example</div>; }

5. Custom Hooks

Custom Hooks allow you to extract and reuse stateful logic across multiple components. They are JavaScript functions whose names start with "use" and may call other Hooks.

Example: useFetch Custom Hook

import { useState, useEffect } from 'react'; import axios from 'axios'; function useFetch(url) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { let isMounted = true; // To prevent setting state on unmounted component axios.get(url) .then(response => { if (isMounted) { setData(response.data); setLoading(false); } }) .catch(err => { if (isMounted) { setError(err); setLoading(false); } }); return () => { isMounted = false; }; }, [url]); return { data, loading, error }; } export default useFetch;

Using the useFetch Hook:

import React from 'react'; import useFetch from './useFetch'; function DataDisplay() { const { data, loading, error } = useFetch('https://api.example.com/data'); if (loading) return <p>Loading data...</p>; if (error) return <p>Error fetching data.</p>; return <div>{JSON.stringify(data)}</div>; } export default DataDisplay;

Key Points:

  • Promote reusability by encapsulating common logic.
  • Enhance readability by abstracting complex logic.
  • Must adhere to the Rules of Hooks.

6. Comparison: Hooks vs. Class Components

With the introduction of Hooks, functional components have become as powerful as class components, often replacing them due to their simplicity and flexibility.

AspectClass ComponentsFunctional Components with Hooks
State ManagementManaged via this.state and setStateManaged via useState, useReducer, etc.
Lifecycle MethodsMethods like componentDidMount, componentDidUpdateManaged via useEffect and other Hooks
SyntaxRequires class syntax, render() methodSimpler function syntax, directly returns JSX
this BindingRequires explicit binding of this in methodsNo this keyword, eliminating binding issues
ReusabilityLimited to higher-order components and render propsEnhanced through custom Hooks
BoilerplateMore verbose, requires constructors and bindingsLess verbose, cleaner and more concise

Example Comparison:

Class Component:

import React, { Component } from 'react'; class Counter extends Component { constructor(props) { super(props); this.state = { count: 0 }; this.increment = this.increment.bind(this); } increment() { this.setState(prevState => ({ count: prevState.count + 1 })); } render() { return ( <div> <p>Count: {this.state.count}</p> <button onClick={this.increment}>Increment</button> </div> ); } } export default Counter;

Functional Component with Hooks:

import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); // State management const increment = () => { setCount(prevCount => prevCount + 1); // State update }; return ( <div> <p>Count: {count}</p> <button onClick={increment}>Increment</button> </div> ); } export default Counter;

7. Best Practices for Using Hooks

  1. Follow the Rules of Hooks: Always call Hooks at the top level and from React functions.

  2. Use Descriptive Hook Names: Name custom Hooks clearly to indicate their purpose, starting with "use".

  3. Manage Dependencies Carefully: Ensure that all dependencies are included in the dependency array of Hooks like useEffect and useCallback to avoid bugs.

  4. Avoid Overusing State: Keep state minimal and only include what's necessary to drive the UI.

  5. Memoize Expensive Functions: Use useMemo and useCallback to prevent unnecessary computations and re-creations of functions.

  6. Encapsulate Reusable Logic: Extract common logic into custom Hooks to promote reusability and cleaner components.

  7. Clean Up Effects: Always return cleanup functions in useEffect to prevent memory leaks, especially when dealing with subscriptions or timers.

  8. Leverage Existing Hooks Libraries: Utilize libraries like React Query or Recoil for advanced state and data management needs.

8. Common Mistakes with Hooks

  1. Violating Hook Rules: Calling Hooks inside loops, conditions, or nested functions can lead to unpredictable behavior.

  2. Missing Dependencies: Forgetting to include necessary dependencies in the dependency array can cause stale data or unintended side effects.

  3. Overusing useState for Complex State: For complex state logic, prefer useReducer to maintain clarity and manageability.

  4. Ignoring Cleanup: Not cleaning up side effects can lead to memory leaks and unintended behaviors.

  5. Improperly Naming Custom Hooks: Custom Hooks must start with "use" to follow conventions and enable linting tools to identify them.

9. Advanced Hooks Usage

a. Custom Hooks

Custom Hooks allow you to create reusable logic that can be shared across multiple components.

Example: useToggle Custom Hook

import { useState } from 'react'; function useToggle(initialValue = false) { const [value, setValue] = useState(initialValue); const toggle = () => setValue(prev => !prev); return [value, toggle]; } export default useToggle;

Using the useToggle Hook:

import React from 'react'; import useToggle from './useToggle'; function ToggleComponent() { const [isOn, toggleIsOn] = useToggle(); return ( <div> <p>The switch is {isOn ? 'ON' : 'OFF'}</p> <button onClick={toggleIsOn}>Toggle</button> </div> ); } export default ToggleComponent;

b. Combining Multiple Hooks

Hooks can be combined to build complex and feature-rich components.

Example: useFetch Hook with useState and useEffect

import { useState, useEffect } from 'react'; import axios from 'axios'; function useFetch(url) { const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { let isMounted = true; // To prevent setting state on unmounted component setLoading(true); axios.get(url) .then(response => { if (isMounted) { setData(response.data); setLoading(false); } }) .catch(err => { if (isMounted) { setError(err); setLoading(false); } }); return () => { isMounted = false; }; }, [url]); return { data, loading, error }; } export default useFetch;

Using the useFetch Hook:

import React from 'react'; import useFetch from './useFetch'; function DataDisplay() { const { data, loading, error } = useFetch('https://api.example.com/data'); if (loading) return <p>Loading...</p>; if (error) return <p>Error loading data.</p>; return <div>{JSON.stringify(data)}</div>; } export default DataDisplay;

10. Comparison: Hooks vs. Other State Management Libraries

While Hooks provide powerful state and side-effect management within components, they can be complemented by external state management libraries for more complex scenarios:

  • Redux: A predictable state container for JavaScript apps. It can be used alongside Hooks like useSelector and useDispatch.

  • MobX: A library that makes state management simple and scalable by transparently applying functional reactive programming.

  • Recoil: A state management library for React that provides a better developer experience and more flexibility than Redux.

Hooks can work seamlessly with these libraries, enhancing their capabilities and simplifying their integration into functional components.

11. Conclusion

Hooks have transformed React development by enabling functional components to handle state, side effects, and other React features that were previously exclusive to class components. They promote cleaner, more readable code, enhance reusability through custom Hooks, and simplify state management. By adhering to the Rules of Hooks and leveraging both built-in and custom Hooks effectively, developers can build robust, efficient, and maintainable React applications.

Key Takeaways:

  • Hooks Enable Functional Component Power: Manage state and side effects without needing class components.

  • Built-In Hooks Cover Common Needs: useState, useEffect, useContext, useReducer, and others provide essential functionalities.

  • Custom Hooks Promote Reusability: Encapsulate and share complex logic across components.

  • Rules of Hooks Ensure Predictable Behavior: Follow best practices to maintain consistency and avoid bugs.

  • Hooks Enhance Developer Experience: Simplify component logic, reduce boilerplate, and improve code maintainability.

Mastering Hooks is essential for modern React development, unlocking the full potential of functional components and enabling the creation of dynamic, efficient, and scalable user interfaces.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How do I fix a Git detached head?
Why are employees leaving Apple?
How to find all occurrences of a key in nested dictionaries and lists?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.