DEV Community

Cover image for Mastering 'useCallback' in React with TypeScript: 4 Different Use-Cases for 'useCallback'
Kirubel Kinfe
Kirubel Kinfe

Posted on • Updated on

Mastering 'useCallback' in React with TypeScript: 4 Different Use-Cases for 'useCallback'

In React, the useCallback hook is a powerful tool that can help optimize your applications by memoizing functions and preventing unnecessary renders. When used in conjunction with TypeScript, it provides type safety and enhances the developer experience. In this article, we'll explore the various use cases of useCallback in React, with a focus on best practices for TypeScript projects.

Understanding useCallback
The useCallback hook is part of the React Hooks API and is designed to memoize functions. It takes two arguments: the function you want to memoize and an array of dependencies. The hook returns a memoized version of the function that remains consistent across renders as long as the dependencies do not change.

Here's a basic example:

import React, { useState, useCallback } from 'react';

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

  const increment = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, the increment function is wrapped in useCallback. It depends on the count state, so it's passed as a dependency in the array. This ensures that the increment function remains the same across renders as long as count doesn't change.

Use Cases for useCallback in React and TypeScript
1. Preventing Unnecessary Renders
One of the primary use cases for useCallback is to prevent unnecessary renders in child components. When a function is not memoized, it's recreated every time the component renders, even if its dependencies haven't changed. This can lead to performance issues in large applications.

import React, { useState, useCallback } from 'react';

function ParentComponent() {
  const [count, setCount] = useState<number>(0);

  const increment = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <ChildComponent increment={increment} />
    </div>
  );
}

function ChildComponent({ increment }: { increment: () => void }) {
  return <button onClick={increment}>Increment</button>;
}
Enter fullscreen mode Exit fullscreen mode

By using useCallback in the parent component, we ensure that the increment function remains the same between renders. This prevents unnecessary re-renders of the ChildComponent.

2. Optimizing Performance in Memoization
In scenarios where you need to memoize expensive calculations or functions, useCallback is essential. It ensures that the function is only recalculated when its dependencies change, saving computational resources.

import React, { useState, useCallback } from 'react';

function ExpensiveComponent() {
  const [count, setCount] = useState<number>(0);
  const [result, setResult] = useState<number | null>(null);

  const calculateExpensiveValue = useCallback(() => {
    setResult(count * 2);
  }, [count]);

  return (
    <div>
      <p>Result: {result}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={calculateExpensiveValue}>Calculate</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, the calculateExpensiveValue function is memoized with useCallback. It only recalculates when the count state changes, preventing unnecessary calculations.

3. Passing Callbacks to Child Components
When passing callback functions as props to child components, it's important to memoize them with useCallback. This ensures that the child components won't re-render unless the dependencies change.

import React, { useState, useCallback } from 'react';

function ParentComponent() {
  const [count, setCount] = useState<number>(0);

  const increment = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return (
    <div>
      <p>Count: {count}</p>
      <ChildComponent increment={increment} />
    </div>
  );
}

function ChildComponent({ increment }: { increment: () => void }) {
  return <button onClick={increment}>Increment</button>;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the increment function is memoized with useCallback in the parent component before being passed to the child component. This ensures that the child component doesn't re-render unnecessarily when the parent component updates.

4. Custom Hooks
useCallback is also useful when creating custom hooks that return functions. By memoizing these functions, you can ensure that they don't change between renders when used in different components.

import React, { useState, useCallback } from 'react';

function useIncrement(initialValue: number): [number, () => void] {
  const [count, setCount] = useState<number>(initialValue);
  const increment = useCallback(() => {
    setCount(count + 1);
  }, [count]);

  return [count, increment];
}

function Counter() {
  const [count, increment] = useIncrement(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, the increment function returned by the useIncrement custom hook is memoized with useCallback, ensuring its stability across renders.

Conclusion
The useCallback hook in React, when combined with TypeScript, is a valuable tool for optimizing performance and preventing unnecessary renders. Understanding its various use cases is essential for building efficient and maintainable React applications. Whether you're preventing re-renders, optimizing performance, or creating custom hooks, useCallback is a versatile hook that plays a key role in enhancing your React components' performance and responsiveness.

Top comments (3)

Collapse
 
coma profile image
Eduardo García Sanz • Edited

You can keep the same reference across all renders taking advantage of the fact that setCount won´t change either and that you can pass a callback to it:

const increment = useCallback(() => {
  setCount(count => count + 1);
}, []);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cyberfiducia profile image
CyberFiducia

Good section on passing callbacks to child components.
Thanks!

Collapse
 
leenodeh12 profile image
Leen Odeh

Thank you!