我有一组id,我创建了一个变异,允许我只使用1个id删除一个项目。
有没有办法使用Relay.Store.commitUpdate
或this.props.relay.commitUpdate
多次调用此突变?
答案 0 :(得分:3)
我认为你可以将每个Relay.Store.commitUpdate包装在Promise中:
commitMutationPromise = (Mutation, data) =>
new Promise((resolve, reject) => {
Relay.Store.commitUpdate(new Mutation(data), {
onSuccess: (transaction) => {
resolve(transaction);
},
onFailure: (transaction) => {
reject(transaction);
},
});
}
并将您的突变作为承诺数组提交并使用Promise.all捕获结果(但请记住其快速失败行为)。 它可能是这样的:
handleDelete = (deleteIds) => {
const deletePromisesArray = [];
deleteIds.forEach(id => {
deletePromisesArray.push(
this.commitMutationPromise(DeleteMutation, { id })
);
});
Promise.all(deletePromisesArray).then(values => {
this.onSuccessDelete(result);
}, error => {
this.onFailDelete(error);
});
}