Function useGenerator

Access a generator function from within a component.

const generator = useGenerator(generatorKey)

The generator is an async function that process and returns data required by your components, such as geometry for a mesh.

const [geometry, setGeometry] = useState<BufferGeometry | null>(null)

useEffect(() => {
if (generator) {
generator(id).then(response => {
if (response) {
const bufferGeometry = unpackBufferGeometry(response)
setGeometry(prev => {
if (prev) prev.dispose()
return bufferGeometry
})
} else {
setGeometry(null)
}
})
}
}, [generator, id])

if (!geometry) return null

return (
<mesh geometry={geometry}>
<meshBasicMaterial />
</mesh>
)
  • Type Parameters

    • T

    Parameters

    • generator: string

    Returns (...args: any[]) => Promise<T> | () => Promise<null>