1.0.2 • Published 12 months ago

pinia-plugin-on-mounted v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

If you need this hook, you're probably using Pinia incorrectly. See this comment!

🍍 Deprecated pinia-plugin-on-mounted

npm bundle size npm npm changelog

ci publish codecov

Don't worry about whether Pinia is initialized, just use your store.

Safely using the Pinia store outside of components.

There are some cases where you need to invoke callbacks when Pinia is mounted. This module can be used in such situations.

🚀 Quick Start

1. Install the plugin

npm i pinia-plugin-on-mounted
yarn add pinia-plugin-on-mounted # for yarn
pnpm add pinia-plugin-on-mounted # for pnpm

2. Add the plugin the Pinia

import { createPinia } from 'pinia';
import { PiniaPluginOnMounted } from 'pinia-plugin-on-mounted';

const pinia = createPinia();
pinia.use(PiniaPluginOnMounted);

3. Use the plugin

import { onPiniaMounted } from 'pinia-plugin-on-mounted';

onPiniaMounted(() => {
  // do something
});

📖 Use cases

1. Don't care if Pinia is initialized

import { onPiniaMounted } from 'pinia-plugin-on-mounted';
import { useUserStore } from './user';

// Pinia initialization errors may occur
// like "Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?"
const userStore = useUserStore();

onPiniaMounted(() => {
  // No matter what you do
  const userStore = useUserStore();
});

You can use your store outside of the store without worrying about whether or not Pinia is initialized.

🌮 API

onPiniaMounted(callback: () => void): void

Register a callback to be called when the Pinia is mounted.

License

MIT