reactive-polling
Reactive Polling
Poll for changes to any value and trigger React re-renders when it changes.
Technique
Create a useReactive hook that uses useFrame to periodically check a selector function. When the value changes, update React state to trigger a re-render. Throttle with a configurable FPS.
Key Concepts
- Selector function returns the value to watch
- Compare with previous value to detect changes
- Only update state when value actually changes
- Throttle polling with
useFrame'sfpsoption - Use sparingly for values that don't change frequently
Usage
const useReactive = <T,>(selector: () => T, fps = 30): T => {
const [reactiveValue, setReactiveValue] = useState<T>(selector())
const previousValueRef = useRef(reactiveValue)
useFrame(
() => {
const newValue = selector()
if (previousValueRef.current !== newValue) {
previousValueRef.current = newValue
setReactiveValue(newValue)
}
},
{ fps }
)
return reactiveValue
}
// Usage
const isAboveZero = useReactive(() => position.y > 0)
This skill is part of verekia's r3f-gamedev.
More from verekia/r3f-gamedev
smooth-interpolation
Animate values smoothly using exponential decay instead of linear interpolation.
12ui-useframe
Sync UI elements outside the Canvas with the render loop using R3F v10's external useFrame.
8r3f-setup
Set up a React Three Fiber project with WebGPU support.
7miniplex
Use Miniplex for minimalistic Entity Component System with TypeScript support.
6bone-attachment
Attach meshes to bones of a skinned mesh, such as attaching a sword to a character's hand.
6verekia-architecture
Day-to-day coding style and patterns for R3F game development with Miniplex ECS.
6