1.0.4 • Published 6 years ago

use-set-interval v1.0.4

Weekly downloads
386
License
ISC
Repository
gitlab
Last release
6 years ago

useSetInterval

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

Note: this was published and didn't work a few days before Dan published this Making setInterval Declarative with React Hooks, which saved my bacon. Many thanks.

Synopsis

In your React component, increment a counter every second:

import { useState } from 'react'
import useSetInterval from "use-set-interval"

function SetInterval() {
  const [ count, setCount ] = useState(0)

  useSetInterval(() => setCount(count + 1), 1000)

  return <p>Count = { count }</p>
}

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

Please note that the interval is cleared and reset if you change the interval ms but not if you change the function fn. This is a good thing, since it allows you to change the interval in a declarative way, instead of having to fiddle with the interval IDs yourself.

Usage

useSetInterval(fn, delay)
  • fn is a function to execute
  • delay is the delay between executions 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)

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago