我正在尝试实现一个自定义钩子来为应用程序提供访客购物车。我的钩子环绕着useMutation
来自 Apollo 的钩子,它将购物车 ID 保存在 cookie 中,同时还提供了“重置”购物车的功能(基本上,在下订单时删除 cookie)。
代码时间!(为简洁起见省略了一些代码):
export const useGuestCart = () => {
let cartId;
const [createCart, { data, error, loading }] = useMutation(MUTATION_CREATE_CART);
console.log(`Hook!`);
if (!cartId || cartId.length === 0) {
createCart();
}
if (loading) {
console.log(`Still loading`);
}
if (data) {
console.log(`Got cart id ${data.createEmptyCart}`);
cartId = data.createEmptyCart;
}
const resetGuestCart = useCallback(() => {
// function body here
});
return [cartId, resetGuestCart];
};
在我的组件中,我只是使用let [cartId, resetCart] = useGuestCart();
.
当我运行单元测试(使用 Apollo 提供模拟突变)时,我看到钩子被调用了几次,输出看起来像这样:
console.log src/utils/hooks.js:53
Hook!
console.log src/utils/hooks.js:53
Hook!
console.log src/utils/hooks.js:59
Still loading
console.log src/utils/hooks.js:53
Hook!
console.log src/utils/hooks.js:62
Got cart id guest123
console.log src/utils/hooks.js:53
Hook!
console.log src/utils/hooks.js:53
Hook!
我只是开始使用钩子,所以我仍然无法掌握它们的工作方式。为什么这么多的钩子调用?
谢谢您的回复!