trela
trela copied to clipboard
[Feature Update]: read API behavior / read APIの挙動
read APIの挙動について
read APIは、非同期処理を実行し結果をキャッシュして、実行されたコンポーネントを描画更新する。
実行する非同期処理が実行中の場合やキャッシュされた値がある場合は、非同期処理を実行せず既に実行済みの結果をキャッシュした値を返す。
const SampleComponent = () => {
const { apis } = useTrela();
const [ result, isLoading ] = apis.anyAsyncFunc().read();
const [ result2, isLoading2 ] = apis.anyAsyncFunc().read(); // 非同期処理は実行されない
if( result === result2 ) console.log( "この二つの値は同じ値" )
/* ... */
}
またキャッシュした値を返す時は、コンポーネントの描画更新はしない。 つまり、read APIは非同期処理が初めて実行され、完了した時のみにコンポーネントを描画更新する。
read APIの型
type ReadAPI<T> = () => [ T | null, boolean ]
- 配列の0番目の値は、
nullか非同期処理の結果値が入る。 - 配列の1番目の値は、その非同期処理が実行中かの真偽値を返す。
- true : 実行中
- false : 実行中でない