videx-3d
    Preparing search index...

    Function useGenerator

    • Access a generator function from within a component.

      Type Parameters

      • T

      Parameters

      • generator: string
      • priority: number = 0

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

      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>
      )