react-three-fiber icon indicating copy to clipboard operation
react-three-fiber copied to clipboard

Make useAsset return a thenable

Open alexandernanberg opened this issue 11 months ago • 2 comments
trafficstars

Currently useAsset suspends by itself. While this is fine for most cases, but there are cases where you want more control. My proposal is that useAsset instead return a Thenable which consumers unwrap with use().

E.g.

const assetPromise = useAsset(GLTFLoader, '/path/to/model.glb')
const asset = use(assetPromise)

or a one-liner

const asset = use(useAsset(GLTFLoader, '/path/to/model.glb'))

Note that I we do this, useAsset doesn't really need to be a hook any more. So we could even do something like

const asset = use(loadAsset(GLTFLoader, '/path/to/model.glb'))

alexandernanberg avatar Dec 07 '24 10:12 alexandernanberg

From Discord:

In v9 we aren't doing breaking changes, but I think this makes sense if we add a new API like loadAsset and then decide if we want to deprecate useAsset later. There is a complete overhaul of caching planned for v10 that this might lead to nicely.

krispya avatar Dec 13 '24 15:12 krispya

useLoader.preload currently returns void, but maybe it returning the cached promise would help here?

CodyJasonBennett avatar Jan 09 '25 11:01 CodyJasonBennett