0
0
Vueframework~20 mins

Store-to-store interaction in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pinia Store Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does one Vue store update another store's state?

Given two Vue stores using Pinia, useStoreA and useStoreB, how can useStoreA update a state property in useStoreB?

Vue
import { defineStore } from 'pinia';

export const useStoreA = defineStore('storeA', {
  state: () => ({ countA: 0 }),
  actions: {
    incrementB() {
      // What goes here?
    }
  }
});

export const useStoreB = defineStore('storeB', {
  state: () => ({ countB: 0 }),
  actions: {
    increment() {
      this.countB++;
    }
  }
});
Aimport { useStoreB } from './storeB'; useStoreB.increment();
Bconst storeB = useStoreB(); storeB.increment();
CuseStoreB.increment();
Dthis.$storeB.increment();
Attempts:
2 left
💡 Hint

Remember to call the other store's function by first creating its instance inside the action.

state_output
intermediate
1:30remaining
What is the value of storeB.countB after calling storeA.incrementB() twice?

Assuming the stores from the previous challenge, what will be the value of storeB.countB after calling storeA.incrementB() two times?

Vue
const storeA = useStoreA();
const storeB = useStoreB();

storeA.incrementB();
storeA.incrementB();

console.log(storeB.countB);
A0
Bundefined
C1
D2
Attempts:
2 left
💡 Hint

Each call to incrementB() calls storeB.increment() once.

📝 Syntax
advanced
2:30remaining
Which option correctly imports and uses another store inside a Pinia store?

Identify the correct syntax to import and use useStoreB inside useStoreA for store-to-store interaction.

A
import useStoreB from './storeB';

export const useStoreA = defineStore('storeA', {
  actions: {
    updateB() {
      useStoreB.countB = 5;
    }
  }
});
B
export const useStoreA = defineStore('storeA', {
  actions: {
    updateB() {
      const storeB = require('./storeB');
      storeB.countB = 5;
    }
  }
});
C
import { useStoreB } from './storeB';

export const useStoreA = defineStore('storeA', {
  actions: {
    updateB() {
      const storeB = useStoreB();
      storeB.countB = 5;
    }
  }
});
D
import { useStoreB } from './storeB';

export const useStoreA = defineStore('storeA', {
  actions: {
    updateB() {
      useStoreB.countB = 5;
    }
  }
});
Attempts:
2 left
💡 Hint

Remember to import the store as a named import and call it as a function to get the instance.

🔧 Debug
advanced
2:30remaining
Why does this store-to-store update not reflect in the UI?

Consider two Pinia stores where storeA updates storeB's state directly. The UI bound to storeB.countB does not update after the change. Why?

Vue
export const useStoreA = defineStore('storeA', {
  actions: {
    updateB() {
      const storeB = useStoreB();
      storeB.countB = 10; // direct assignment
    }
  }
});
ADirect assignment to state property bypasses Vue reactivity; must use a Pinia action to update state.
BstoreB instance is not reactive because it was not created with defineStore.
CUI does not update because storeB.countB is a computed property and cannot be assigned.
DPinia does not support store-to-store interaction; state cannot be shared.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes and updates the UI.

🧠 Conceptual
expert
3:00remaining
What is the best practice for sharing state between two Pinia stores to keep them decoupled?

You want two Pinia stores to share some state but keep them loosely coupled. Which approach is best?

ACreate a third shared store that holds the common state; both stores import and use it.
BDirectly import one store inside the other and modify its state directly.
CUse global variables outside Pinia to hold shared state accessed by both stores.
DDuplicate the shared state in both stores and synchronize manually with watchers.
Attempts:
2 left
💡 Hint

Think about modularity and avoiding tight coupling.