UseTicker manages a ticker lifecycle within a component. It creates a ticker that calls the provided function at regular intervals. The ticker is automatically stopped on dependency changes or component unmount. This hook must be called within a component context.
(interval time.Duration, tickFn func(), deps []any)
| 189 | // The ticker is automatically stopped on dependency changes or component unmount. |
| 190 | // This hook must be called within a component context. |
| 191 | func UseTicker(interval time.Duration, tickFn func(), deps []any) { |
| 192 | UseGoRoutine(func(ctx context.Context) { |
| 193 | ticker := time.NewTicker(interval) |
| 194 | defer ticker.Stop() |
| 195 | |
| 196 | for { |
| 197 | select { |
| 198 | case <-ctx.Done(): |
| 199 | return |
| 200 | case <-ticker.C: |
| 201 | tickFn() |
| 202 | } |
| 203 | } |
| 204 | }, deps) |
| 205 | } |
| 206 | |
| 207 | // UseAfter manages a timeout lifecycle within a component. |
| 208 | // It creates a timer that calls the provided function after the specified duration. |