waitForAll(依存関係)
複数の非同期依存関係を同時に評価できるコンカレンシーヘルパーです。
依存関係は、タプル配列として、またはオブジェクトの名付き依存関係として提供できます。
function waitForAll(dependencies: Array<RecoilValue<>>):
RecoilValueReadOnly<UnwrappedArray>
function waitForAll(dependencies: {[string]: RecoilValue<>}):
RecoilValueReadOnly<UnwrappedObject>
このコンカレンシーヘルパーはセレクタとして提供されるため、React コンポーネントの Recoil フック、Recoil セレクタの依存関係、または Recoil ステートが使用される任意の場所で利用できます。
例
function FriendsInfo() {
const [friendA, friendB] = useRecoilValue(
waitForAll([friendAState, friendBState])
);
return (
<div>
Friend A Name: {friendA.name}
Friend B Name: {friendB.name}
</div>
);
}
const friendsInfoQuery = selector({
key: 'FriendsInfoQuery',
get: ({get}) => {
const {friendList} = get(currentUserInfoQuery);
const friends = get(waitForAll(
friendList.map(friendID => userInfoQuery(friendID))
));
return friends;
},
});
const customerInfoQuery = selectorFamily({
key: 'CustomerInfoQuery',
get: id => ({get}) => {
const {info, invoices, payments} = get(waitForAll({
info: customerInfoQuery(id),
invoices: invoicesQuery(id),
payments: paymentsQuery(id),
}));
return {
name: info.name,
transactions: [
...invoices,
...payments,
],
};
},
});