https://github.com/ckaznable/react-use-shared-tick
React hook run function in same setInterval
https://github.com/ckaznable/react-use-shared-tick
react react-hooks typescript typescript-library
Last synced: 3 months ago
JSON representation
React hook run function in same setInterval
- Host: GitHub
- URL: https://github.com/ckaznable/react-use-shared-tick
- Owner: ckaznable
- License: mit
- Created: 2023-01-07T06:35:16.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-02T05:23:38.000Z (about 2 years ago)
- Last Synced: 2025-02-13T11:04:54.316Z (over 1 year ago)
- Topics: react, react-hooks, typescript, typescript-library
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/react-use-shared-tick
- Size: 85 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/react-use-shared-tick)
# React useSharedTick
React hook run function in same `setInterval`
## Installation
use npm
```
npm install react-use-shared-tick --save
```
use yarn
```
yarn add react-use-shared-tick
```
use pnpm
```
pnpm install react-use-shared-tick
```
## Usage
run function per second
```js
import { useStatus } from "react"
import useSharedTick from "react-use-shared-tick"
function App() {
const [time, setTime] = useState(Date.now())
useSharedTick(setTime)
return (
Current Timestamp: {time}
)
}
```
run function per 3 second
```jsx
useSharedTick(setTime, {tick: 3000})
```
## API
### useSharedTick
```js
useSharedTick(handler: (t: number)=>void, param)
```
### Handler
| Param | Type | Description |
|---|---|---|
|timestamp|`number`|current timestamp|
### Parameter
| Field | Type | Default | Requirement |Description |
|---|:---:|:---:|:---:|:---:|
| tick | `number` | `1000` | `No` | Running interval time |
| runOnTabVisible | `boolean` | `false` | `No` | when true will pause/start interval if `visibilitychange` event dispatched |
## License
The files included in this repository are licensed under the MIT license.