Module: Hooks Basics

Introduction to Hooks

What are Hooks?

Hooks are a new addition in React 16.8. They let you use state and other React featureswithout writing a class. Essentially, they allow functional components to "hook into" React state and lifecycle features that were previously only available in class components.

Before Hooks, you needed class components to manage state, lifecycle methods (likecomponentDidMount), and other features. Hooks provide a more concise and readable way to achieve the same functionality using functional components.

Why Use Hooks?

  • Simpler Code:Functional components with Hooks are more concise.
  • Reusability:Hooks allow logic to be reused across components.
  • AvoidsthisConfusion:No need to bindthis.
  • Easier to Test:Functional components are easier to test.
  • Better Organization:Logic is grouped by purpose.

The Problem Hooks Solve (Before Hooks)

Consider a typical class component:


import React from 'react';

class ExampleComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  incrementCount = () => {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.incrementCount}>Increment</button>
      </div>
    );
  }
}

export default ExampleComponent;

This component requires:

  • A class declaration
  • A constructor to initialize state
  • thisbinding
  • setStatefor updates

Hooks allow the same result with a functional component:


import React, { useState } from 'react';

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

  const incrementCount = () => {
    setCount(count + 1);
  };

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

export default ExampleComponent;

Notice how much cleaner and more straightforward this version is.

Built-in Hooks

React provides several built-in Hooks:

  • useState:Manage component state
  • useEffect:Handle side effects
  • useContext:Access React context
  • useReducer:Manage complex state logic
  • useCallback:Memoize functions
  • useMemo:Memoize expensive calculations
  • useRef:Access DOM nodes or mutable values
  • useImperativeHandle:Customize refs
  • useLayoutEffect:Synchronous effects after DOM updates
  • useDebugValue:Debug custom Hooks

Rules of Hooks

Hooks have rules thatmustbe followed:

  1. Only Call Hooks at the Top Level:No loops or conditions.
  2. Only Call Hooks from Function Components:Or custom Hooks.
  3. Don't Return Anything from Hooks:Hooks manage logic, not output.

Breaking these rules leads to errors. React’s ESLint plugin helps enforce them.

Creating Custom Hooks

Custom Hooks allow you to extract and reuse component logic across multiple components. This is one of the most powerful features of Hooks.

Next Steps

This is just an introduction to Hooks. Next, you’ll dive deeper into Hooks likeuseStateanduseEffect, and learn how to create custom Hooks for reusable and maintainable React components.