use-set-timeout
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/use-set-timeout package

1.0.4 • Public • Published

useSetTimeout

A React Hook (using useEffect) which uses window.setTimeout().

Synopsis

In your React component:

import { useState } from 'react'
import useSetTimeout from "use-set-timeout"

function SetTimeout() {
  const [ triggered, setTriggered ] = useState(false)

  useSetTimeout(() => { setTriggered(true) }, 5 * 1000)

  return <p>triggered = { triggered }</p>
}

There is no need to keep a reference to the timeout ID since it will be automatically cleared when the component unmounts.

Please note that the timeout is cleared and reset if you change the timeout ms but not if you change the function fn.

Usage

useSetTimeout(fn, delay)
  • fn is a function to execute
  • delay is the delay in milliseconds

Other Hooks

Please see all of the other reacthooks.org hooks:

Author

$ npx chilts

   ╒════════════════════════════════════════════════════╕
   │                                                    │
   │   Andrew Chilton (Personal)                        │
   │   -------------------------                        │
   │                                                    │
   │          Email : andychilton@gmail.com             │
   │            Web : https://chilts.org                │
   │        Twitter : https://twitter.com/andychilton   │
   │         GitHub : https://github.com/chilts         │
   │         GitLab : https://gitlab.org/chilts         │
   │                                                    │
   │   Apps Attic Ltd (My Company)                      │
   │   ---------------------------                      │
   │                                                    │
   │          Email : chilts@appsattic.com              │
   │            Web : https://appsattic.com             │
   │        Twitter : https://twitter.com/AppsAttic     │
   │         GitLab : https://gitlab.com/appsattic      │
   │                                                    │
   │   Node.js / npm                                    │
   │   -------------                                    │
   │                                                    │
   │        Profile : https://www.npmjs.com/~chilts     │
   │           Card : $ npx chilts                      │
   │                                                    │
   ╘════════════════════════════════════════════════════╛

(Ends)

/use-set-timeout/

    Package Sidebar

    Install

    npm i use-set-timeout

    Weekly Downloads

    16

    Version

    1.0.4

    License

    ISC

    Unpacked Size

    5.94 kB

    Total Files

    4

    Last publish

    Collaborators

    • chilts-appsattic