Skip to content

usePrevious #526

@jsartisan

Description

@jsartisan

Info

difficulty: easy
title: usePrevious
type: question
template: react
tags: react

Question

Implement a custom React hook called usePrevious that stores the previous value of a given state or prop.

Template

template.react.md

import { useState } from "react";
import { usePrevious } from "./usePrevious";

export default function App() {
  const [count, setCount] = useState(0);
  const prevCount = usePrevious(count);

  return (
    <div>
      <p>Current: {count}</p>
      <p>Previous: {prevCount}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
    </div>
  );
}
export function usePrevious(value) {
  
} 

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions