Skip to content

Files

Latest commit

 

History

History
36 lines (25 loc) · 792 Bytes

useUpdateEffect.md

File metadata and controls

36 lines (25 loc) · 792 Bytes

useUpdateEffect

A hook to run an effect only on updates, not on the initial mount.

Arguments

  • callback (function): The function to call on updates.
  • dependencies (array): The dependencies array for the effect.

Returns

  • void

Hooks Involved

How to Use

import { useState } from "react"
import useUpdateEffect from "./useUpdateEffect"

export default function UpdateEffectComponent() {
    const [count, setCount] = useState(10)
    useUpdateEffect(() => alert(count), [count])

    return (
        <div>
            <div>{count}</div>
            <button onClick={() => setCount(c => c + 1)}>Increment</button>
        </div>
    )
}