- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Mastering useRef with NodeJS.Timeout in React + TypeScript
React Hooks have transformed how we manage state and behavior in functional components. Among them, useRef is often underestimated—especially when working with timers, intervals, and side-effect synchronization.
In this article, we’ll deep dive into how to master useRef<NodeJS.Timeout> using TypeScript, explore its overloads, and build a mathematical timer-driven example to demonstrate its full potential.
What Is useRef<NodeJS.Timeout>?
When working in a TypeScript environment, especially in NodeJS or with browser-based timers like setTimeout and setInterval, TypeScript can infer a complex type.
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
This line tells TypeScript:
- We are storing a reference to a timer (from setTimeout).
- timeoutRef.current will hold the timer ID and persist between renders.
- The reference doesn’t trigger re-renders—perfect for performance!
Using useState for timers leads to re-renders and can create memory leaks or logical bugs. Use useRef instead to hold timers or DOM references.
Real-World Math Example: Sine Wave Trigger
Let’s build a visual simulation that shows a new value of a sine wave every X milliseconds. We’ll use:
- useRef<NodeJS.Timeout> to handle interval
- useEffect to set up and clean timers
- useState to render new sine values
import React, { useEffect, useRef, useState } from 'react';
export const SineWaveTimer = () => {
const [angle, setAngle] = useState(0);
const [value, setValue] = useState(0);
const timerRef = useRef<NodeJS.Timeout | null>(null);
useEffect(() => {
timerRef.current = setInterval(() => {
setAngle(prev => {
const nextAngle = prev + 15;
setValue(Math.sin(nextAngle * Math.PI / 180));
return nextAngle;
});
}, 1000);
return () => {
if (timerRef.current) clearInterval(timerRef.current);
};
}, []);
return (
<div className="math-card">
<h3>? Sine Timer Simulation</h3>
<p>Angle: {angle} degrees</p>
<p>Sine Value: {value.toFixed(4)}</p>
</div>
);
};
What Makes useRef<NodeJS.Timeout> Powerful?
| Feature | Benefit |
|---|---|
| Persistence | Retains value across renders without triggering re-renders |
| Memory Safety | clearTimeout / clearInterval managed on cleanup |
| Type Safety | NodeJS / browser distinction managed via types |
- Always initialize useRef(null) for cleanup-safe access.
- Use typeof window !== 'undefined' if targeting both NodeJS and browser.
- Avoid using useRef like a state holder—prefer useState for render-affecting data.
In React + TypeScript, mastering useRef<NodeJS.Timeout> gives you:
- Clean and performant timer management
- Declarative logic with hooks
- No memory leaks or confusing behavior on re-renders
This simple yet powerful tool is essential for building advanced, time-sensitive UI components in React.
Now that you understand the concept, try using useRef in:
- Debouncing input handlers
- Animation loops
- Real-time simulations
- Game development
The clock is ticking—so put your knowledge into action!
#react #typescript #hooks #useref #frontend #timers #performance