Skip to content

getActivePinia()

The getActivePinia() function returns the currently active Pinia instance. This is useful in scenarios where you need to access the current Pinia instance.

Function Signature

ts
function getActivePinia(): Pinia | undefined

Parameters

None.

Return Value

  • Type: Pinia | undefined
  • Description: The currently active Pinia instance, or undefined if no instance is active

Basic Usage

Getting Active Instance

js
import { getActivePinia } from 'pinia'

const pinia = getActivePinia()
if (pinia) {
  console.log('Found active Pinia instance')
} else {
  console.log('No active Pinia instance')
}

Checking Instance Status

js
import { getActivePinia } from 'pinia'

function checkPiniaStatus() {
  const pinia = getActivePinia()
  
  if (!pinia) {
    throw new Error('Pinia instance not initialized')
  }
  
  return pinia
}

Common Use Cases

1. Plugin Development

Getting the current instance when developing Pinia plugins:

js
function myPiniaPlugin() {
  const pinia = getActivePinia()
  
  if (!pinia) {
    console.warn('Plugin requires an active Pinia instance')
    return
  }
  
  // Use pinia instance for plugin logic
  pinia.use((context) => {
    // Plugin logic
  })
}

2. Utility Functions

Creating utility functions that need access to Pinia:

js
import { getActivePinia } from 'pinia'

function getAllStores() {
  const pinia = getActivePinia()
  
  if (!pinia) {
    return []
  }
  
  // Return all registered stores
  return Array.from(pinia._s.values())
}

function getStoreById(id) {
  const pinia = getActivePinia()
  
  if (!pinia) {
    return undefined
  }
  
  return pinia._s.get(id)
}

3. Debugging and Development Tools

js
import { getActivePinia } from 'pinia'

function debugPiniaState() {
  const pinia = getActivePinia()
  
  if (!pinia) {
    console.log('No active Pinia instance')
    return
  }
  
  console.log('Pinia instance info:')
  console.log('- Registered stores:', pinia._s.size)
  console.log('- Plugins:', pinia._p.length)
  console.log('- State:', pinia.state.value)
}

4. Conditional Store Creation

js
import { getActivePinia, createPinia, setActivePinia } from 'pinia'

function ensurePiniaInstance() {
  let pinia = getActivePinia()
  
  if (!pinia) {
    console.log('Creating new Pinia instance')
    pinia = createPinia()
    setActivePinia(pinia)
  }
  
  return pinia
}

Advanced Usage

Instance Monitoring

js
import { getActivePinia } from 'pinia'

class PiniaMonitor {
  constructor() {
    this.checkInterval = null
  }
  
  startMonitoring() {
    this.checkInterval = setInterval(() => {
      const pinia = getActivePinia()
      
      if (pinia) {
        this.logPiniaStats(pinia)
      } else {
        console.warn('Pinia instance lost')
      }
    }, 5000)
  }
  
  stopMonitoring() {
    if (this.checkInterval) {
      clearInterval(this.checkInterval)
      this.checkInterval = null
    }
  }
  
  logPiniaStats(pinia) {
    console.log('Pinia stats:', {
      stores: pinia._s.size,
      plugins: pinia._p.length,
      timestamp: new Date().toISOString()
    })
  }
}

Instance Comparison

js
import { getActivePinia } from 'pinia'

function comparePiniaInstances(expectedPinia) {
  const currentPinia = getActivePinia()
  
  if (!currentPinia) {
    return {
      match: false,
      reason: 'No active Pinia instance'
    }
  }
  
  if (currentPinia === expectedPinia) {
    return {
      match: true,
      reason: 'Instance matches'
    }
  }
  
  return {
    match: false,
    reason: 'Instance mismatch',
    current: currentPinia,
    expected: expectedPinia
  }
}

Safe Access Pattern

js
import { getActivePinia } from 'pinia'

function withActivePinia(callback, fallback = null) {
  const pinia = getActivePinia()
  
  if (!pinia) {
    console.warn('No active Pinia instance, using fallback')
    return fallback
  }
  
  try {
    return callback(pinia)
  } catch (error) {
    console.error('Error using Pinia instance:', error)
    return fallback
  }
}

// Usage example
const storeCount = withActivePinia(
  (pinia) => pinia._s.size,
  0 // fallback value
)

TypeScript Usage

ts
import { Pinia, getActivePinia } from 'pinia'

function getTypedActivePinia(): Pinia {
  const pinia = getActivePinia()
  
  if (!pinia) {
    throw new Error('No active Pinia instance')
  }
  
  return pinia
}

// Type guard
function hasActivePinia(): boolean {
  return getActivePinia() !== undefined
}

// Conditional type
type PiniaOrUndefined = ReturnType<typeof getActivePinia>

function processActivePinia(): string {
  const pinia: PiniaOrUndefined = getActivePinia()
  
  if (pinia) {
    return `Found Pinia instance with ${pinia._s.size} stores`
  }
  
  return 'No active Pinia instance'
}

Error Handling

Safe Checking

js
import { getActivePinia } from 'pinia'

function safeGetActivePinia() {
  try {
    return getActivePinia()
  } catch (error) {
    console.error('Error getting active Pinia instance:', error)
    return undefined
  }
}

Instance Validation

js
import { getActivePinia } from 'pinia'

function validateActivePinia() {
  const pinia = getActivePinia()
  
  if (!pinia) {
    throw new Error('Active Pinia instance required')
  }
  
  if (!pinia._s) {
    throw new Error('Pinia instance corrupted: missing stores map')
  }
  
  if (!pinia._p) {
    throw new Error('Pinia instance corrupted: missing plugins array')
  }
  
  return pinia
}

Best Practices

1. Always Check Return Value

js
// Correct
const pinia = getActivePinia()
if (pinia) {
  // use pinia
}

// Wrong - not checking for undefined
const pinia = getActivePinia()
pinia._s.size // will error if pinia is undefined

2. Call at Appropriate Times

js
// Correct - call after Vue app setup
function afterAppSetup() {
  const pinia = getActivePinia()
  // use pinia
}

// Avoid - calling during module loading
const pinia = getActivePinia() // might not be set yet

3. Cache Results (if appropriate)

js
class PiniaService {
  constructor() {
    this._cachedPinia = null
  }
  
  getPinia() {
    if (!this._cachedPinia) {
      this._cachedPinia = getActivePinia()
    }
    return this._cachedPinia
  }
  
  invalidateCache() {
    this._cachedPinia = null
  }
}

Common Pitfalls

1. Assuming There's Always an Active Instance

js
// Wrong - assuming there's always an instance
function badExample() {
  return getActivePinia()._s.size
}

// Correct - checking instance exists
function goodExample() {
  const pinia = getActivePinia()
  return pinia ? pinia._s.size : 0
}

2. Calling at Wrong Time

js
// Problematic - calling during import
const globalPinia = getActivePinia()

// Better - calling in function
function getCurrentPinia() {
  return getActivePinia()
}

3. Not Handling Instance Changes

js
// Problematic - caching potentially stale instance
const cachedPinia = getActivePinia()

// Better - getting current instance each time
function getCurrentStoreCount() {
  const pinia = getActivePinia()
  return pinia ? pinia._s.size : 0
}

Released under the MIT License.