Hone logo
Hone
Problems

Implementing a usePrevious Hook in React with TypeScript

The usePrevious hook is a custom React hook that allows you to access the previous value of a prop or state variable within a functional component. This is incredibly useful for detecting changes, triggering side effects only when a value changes, or performing comparisons between the current and previous values. Your task is to implement this hook using TypeScript, ensuring type safety and proper handling of initial values.

Problem Description

You need to create a reusable usePrevious hook that accepts a value as input and returns the previous value of that value. The hook should:

  • Accept a value: The hook should take a single argument, value, which represents the value you want to track. This value can be of any type.
  • Store the previous value: Internally, the hook should store the previous value of the input value.
  • Return the previous value: The hook should return the stored previous value.
  • Handle initial render: On the initial render, there is no previous value. The hook should return undefined in this case.
  • Update on re-renders: On subsequent re-renders, the hook should update its stored previous value with the current value of the input value.
  • Type Safety: The hook should be written in TypeScript and maintain type safety for the input value and the returned previous value.

Expected Behavior:

The hook should accurately track the previous value of the input and return it on subsequent renders. It should gracefully handle the initial render by returning undefined.

Examples

Example 1:

Input: value = 1, initial render
Output: undefined
Explanation: On the initial render, there's no previous value, so undefined is returned.

Input: value = 1, next render
Output: 1
Explanation: The previous value (1) is returned.

Input: value = 2, next render
Output: 1
Explanation: The previous value (1) is returned.

Example 2:

Input: value = "hello", initial render
Output: undefined
Explanation: On the initial render, there's no previous value, so undefined is returned.

Input: value = "hello", next render
Output: "hello"
Explanation: The previous value ("hello") is returned.

Input: value = "world", next render
Output: "hello"
Explanation: The previous value ("hello") is returned.

Example 3:

Input: value = { name: "Alice" }, initial render
Output: undefined
Explanation: On the initial render, there's no previous value, so undefined is returned.

Input: value = { name: "Alice" }, next render
Output: { name: "Alice" }
Explanation: The previous value ({ name: "Alice" }) is returned.

Input: value = { name: "Bob" }, next render
Output: { name: "Alice" }
Explanation: The previous value ({ name: "Alice" }) is returned.

Constraints

  • The hook must be implemented using the useState hook.
  • The hook must be written in TypeScript.
  • The hook should be reusable and not depend on any specific component context.
  • The hook should not cause unnecessary re-renders.

Notes

  • Consider using useEffect to manage the storage of the previous value.
  • Think about how to handle different data types correctly.
  • The goal is to create a clean, concise, and type-safe implementation of the usePrevious hook. Focus on correctness and readability.
Loading editor...
typescript