usePrevious

Track the previous value of a variable.

https://www.shaktools.com/shak-hooks
State

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { usePrevious } from "@shak-hooks/usehooks";
Source: packages/react/src/usePrevious.ts

Signature

signature.ts
export function usePrevious<T>(value: T): T | undefined

Parameters

NameTypeOptionalDefault
valueTNo-

Returns

T | undefined

Implementation

use-previous.ts
1import { useEffect, useRef } from "react";
2
3export function usePrevious<T>(value: T): T | undefined {
4 const ref = useRef<T>();
5
6 useEffect(() => {
7 ref.current = value;
8 }, [value]);
9
10 return ref.current;
11}

Advertisement

Google Ads

Usage

example.tsx
1import { usePrevious } from "@shak-hooks/usehooks";
2
3const result = usePrevious(undefined);
4// result: T | undefined
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy