跳到主內容

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,
],
};
},
});