waitForAll(dependencies)
一个并发助手,允许我们并发地评估多个异步依赖项。
依赖项可以作为元组数组提供,也可以作为对象中的命名依赖项提供。
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,
],
};
},
});