Skip to content

Share state between islands #236

Answered by ElMassimo
ohuu asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Oliver!

In the implementation above, useSharedState() creates and returns a new store every time is called, so each island is getting a separate store.

The store should be defined at the module level, which ensures the same store is imported by both islands:

/// useSharedState.ts

const pinia = createPinia()

const useStuffStore = defineStore('stuff', () => {
  const stuff: Ref<string | undefined> = ref(undefined)

  return { stuff }
})

export function useStuff () {
  return useStuffStore(pinia)
}

Alternatively, you can simplify this pattern even further, and not use pinia at all:

/// useSharedState.ts

const stuff: Ref<string | undefined> = ref(undefined);

export function useStuff () 

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ElMassimo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants