Replies: 2 comments 4 replies
-
To avoid the problem of using your addProduct and removeItem functions inside another hook you need to wrap them in React.useCallback. I also recommend you to avoid using revalidate, it's going to be deprecated, instead, use mutate, if you don't pass anything is the same as a revalidation. |
Beta Was this translation helpful? Give feedback.
-
I'll try to setup a sandbox. I realised now that my const { session } = useSession()
const { data: order} = useSWR<Order>(() => [`/order/${session.orderId}`, sessionId], get) I can verify via log outputs that the session indeed changes (after login and after logout). However after login, the order hook is not triggered, even though the |
Beta Was this translation helpful? Give feedback.
-
What is the best way to include mutation handling and swr cache revalidation in a custom hook?
I am currently using this:
It works, but I wonder if this is the recommended way to do that?
It also create a rather unstable hook, as the 2
addProduct
andremoveItem
are not stable in a way they could be used as dependencies for hooks because they are recreated each time the hook is called.Beta Was this translation helpful? Give feedback.
All reactions