Import the store in every component VS import once and pass data through props #2268
Replies: 2 comments
-
Have you found it answer, I have the same question in my mind. |
Beta Was this translation helpful? Give feedback.
-
I would use For example I have this I will always make sure to organize these components in a hierarchy, and then pass the main store only to the parents of their groups, so for example for In <script setup>
import { provide } from 'vue'
import { useStoreA } from '...'
import ComponentA1 from '@...'
const storeA = useStoreA()
provide('storeA', storeA) // only in here
</script>
<template>
<ComponentA1 />
</template> In <script setup>
import { inject } from 'vue'
import ComponentA2 from '...'
const storeA = inject('storeA')
</script>
<template>
{{ storeA.someState }} - {{ storeA.someGetter }}
<ComponentA2 :foo="storeA.otherState" :barHandler="storeA.someAction" />
</template> If I need to have access to some of the states from In my <script setup>
import { provide } from 'vue'
import { useMainStore } from '...'
const appState = useMainStore() // the "big" main store
provide('appState', appState)
</script>
<template>
<slot />
</template> Then for example in ComponentA1 where I need access to certain states from this store, I can just use the But if know which state I want, I will expose that using Pinia's <script setup>
import { provide } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore } from '...'
const appState = useMainStore()
const { state1, state2, ...etc } = storeToRefs(appState)
// I will keep this if I still want to use actions
// in my child components down the road. Yes,
// I can still access states here.
provide('appState', appState)
// but here, I will just put states.
// I can provide only the states I know my components will need.
provide('appButtonStates', {
state1,
state2,
})
</script> In ComponentA1 where I need the access. <script setup>
...
const { state1 } = inject('appState') // ComponentA1 only needs state1
</script>
<template>
<MyButton v-disabled="!state1"></MyButton>
</template> Idk if this is the best practice since I am fairly new to Pinia but it fits my app structure, feels natural enough, and works for me. |
Beta Was this translation helpful? Give feedback.
-
HI, i have a doubt about what would be the best way to use a pinia store in my app.
Let's Imagine i have several stores, and one the them (the main store) holds an object with a big amount of different values. Every component in the app will need a few of them.
I can't think about two different ways to do it:
import the store in the main component and pass needed data to each child component using props (or use provide/inject)
or
use the store in every component importing the "big" object (and avoiding props) , although the components only needs a small part of it
I don't know if import the store so many times could affect performance, so what do you think would be the best approach?
regards
Beta Was this translation helpful? Give feedback.
All reactions