Skip to content

Commit

Permalink
[@mantine/hooks] use-interval: Add autoInvoke option support
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Jul 13, 2024
1 parent 1dfef74 commit 010a4b8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
22 changes: 21 additions & 1 deletion apps/mantine.dev/src/pages/hooks/use-interval.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ export default Layout(MDX_DATA.useInterval);

<Demo data={HooksDemos.useIntervalDemo} />

## Auto invoke interval

To automatically start interval when component is mounted, set `autoInvoke` option to `true`:

```tsx
import { useInterval } from '@mantine/hooks';

const interval = useInterval(
() => console.log('Interval tick'),
1000,
{ autoInvoke: true }
);
```

## API

```tsx
Expand All @@ -31,9 +45,15 @@ Return object:
## Definition

```tsx
interface UseIntervalOptions {
/** If set, the interval will start automatically when the component is mounted, `false` by default */
autoInvoke?: boolean;
}

function useInterval(
fn: () => void,
interval: number
interval: number,
options?: UseIntervalOptions
): {
start: () => void;
stop: () => void;
Expand Down
24 changes: 24 additions & 0 deletions packages/@mantine/hooks/src/use-interval/use-interval.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ export function Usage() {
</div>
);
}

export function AutoInvoke() {
const [counter, setCounter] = useState(0);
const [timeout, setTimeout] = useState(200);
const interval = useInterval(() => setCounter(counter + 1), timeout, { autoInvoke: true });

return (
<div>
<button type="button" style={{ padding: 40 }} onClick={() => setCounter(Math.random())}>
Set counter
</button>
<button
type="button"
style={{ padding: 40 }}
onClick={() => setTimeout((v) => (v === 200 ? 1000 : 200))}
>
Set timeout
</button>
<button type="button" style={{ padding: 40 }} onClick={interval.toggle}>
Counter: {counter}
</button>
</div>
);
}
17 changes: 16 additions & 1 deletion packages/@mantine/hooks/src/use-interval/use-interval.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { useEffect, useRef, useState } from 'react';

export function useInterval(fn: () => void, interval: number) {
interface UseIntervalOptions {
/** If set, the interval will start automatically when the component is mounted, `false` by default */
autoInvoke?: boolean;
}

export function useInterval(
fn: () => void,
interval: number,
{ autoInvoke = false }: UseIntervalOptions = {}
) {
const [active, setActive] = useState(false);
const intervalRef = useRef<number>();
const fnRef = useRef<() => void>();
Expand Down Expand Up @@ -34,5 +43,11 @@ export function useInterval(fn: () => void, interval: number) {
return stop;
}, [fn, active, interval]);

useEffect(() => {
if (autoInvoke) {
start();
}
}, []);

return { start, stop, toggle, active };
}

0 comments on commit 010a4b8

Please sign in to comment.