Skip to main content
Version: 0.10.0

Function: useModel()

useModel<TConfig, TModel>(createModel, config): object

Defined in: hooks/useModel.ts:23

React hook to instantiate and manage a model or task instance with automatic lifetime management.

It manages the lifecycle of a model or task runner instance. When the configuration changes or the component unmounts, it automatically disposes of the previous instance to prevent native memory leaks.

Type Parameters

TConfig

TConfig

The configuration type passed to the model creator.

TModel

TModel extends object

The type of the compiled model instance containing a dispose method.

Parameters

createModel

(config) => Promise<TModel>

An asynchronous factory function to instantiate the model or task runner.

config

The configuration to pass to createModel, or undefined if the model shouldn't be loaded yet. It is tracked by value, so the model is re-created whenever the config's contents change and passing an inline object is safe.

TConfig | undefined

Returns

object

An object containing the loaded model instance and any instantiation error.

error

error: any

model

model: any