react-three-fiber
react-three-fiber copied to clipboard
Make useAsset return a thenable
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'))
From Discord:
In v9 we aren't doing breaking changes, but I think this makes sense if we add a new API like
loadAssetand then decide if we want to deprecateuseAssetlater. There is a complete overhaul of caching planned for v10 that this might lead to nicely.
useLoader.preload currently returns void, but maybe it returning the cached promise would help here?