Skip to content

Instantly share code, notes, and snippets.

@hmans
Created September 4, 2022 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmans/7579f86b22f426023c2b0dabf1a44cf0 to your computer and use it in GitHub Desktop.
Save hmans/7579f86b22f426023c2b0dabf1a44cf0 to your computer and use it in GitHub Desktop.
import { useFrame } from "@react-three/fiber"
import { useRef } from "react"
/* TODO: Extract this into hmans/things or similar */
export function useFrameEffect<T>(
dependencyCallback: () => T,
callback: (args: T) => void,
renderPriority = 0
) {
const value = useRef<T>(null!)
useFrame(() => {
const newValue = dependencyCallback()
if (value.current !== newValue) {
value.current = newValue
callback(newValue)
}
}, renderPriority)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment