xTheWolf
xTheWolf

Reputation: 1886

Is there any downside to using Lifecycle hooks in pinia store actions?

I discovered that this is working without a problem:

Store.ts

 function setParentRoute(route: Route | undefined) {
    onBeforeMount(() => {
      parentRoute.value = route;
    });

    onBeforeUnmount(() => {
      clearParentRoute();
    });
  }

  function clearParentRoute() {
      parentRoute.value = undefined;
  }

If I call the setParentRoute function from within a component, the value for parentRoute is only set before mount, and its removed as the component is destroyed.

This saves me a lot of repetitive code and essentially functions like a mixin back in Vue 2. Is there a downside to using it like this? Or maybe even a better approach?

Upvotes: 0

Views: 380

Answers (0)

Related Questions