Skip to content

Commit a482992

Browse files
authored
docs: clarify import store requirement in Options API usage (#1044)
1 parent db380cf commit a482992

File tree

3 files changed

+143
-15
lines changed

3 files changed

+143
-15
lines changed

packages/docs/core-concepts/actions.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,65 @@ export default {
105105
}
106106
```
107107

108-
## Usage with the options API
108+
## Usage with the Options API
109109

110-
If you are not using the composition API, and you are using `computed`, `methods`, ..., you can use the `mapActions()` helper to map actions properties as methods in your component:
110+
For the following examples, you can assume the following store was created:
111+
112+
```js
113+
// Example File Path:
114+
// ./src/stores/counterStore.js
115+
116+
import { defineStore } from 'pinia',
117+
118+
const useCounterStore = defineStore('counterStore', {
119+
state: () => ({
120+
counter: 0
121+
}),
122+
actions: {
123+
increment() {
124+
this.counter++
125+
}
126+
}
127+
})
128+
```
129+
130+
### With `setup()`
131+
132+
While Composition API is not for everyone, the `setup()` hook can make using Pinia easier to work with in the Options API. No extra map helper functions needed!
133+
134+
```js
135+
import { useCounterStore } from '../stores/counterStore'
136+
137+
export default {
138+
setup() {
139+
const counterStore = useCounterStore()
140+
141+
return { counterStore }
142+
},
143+
methods: {
144+
incrementAndPrint() {
145+
counterStore.increment()
146+
console.log('New Count:', counterStore.count)
147+
},
148+
},
149+
}
150+
```
151+
152+
### Without `setup()`
153+
154+
If you would prefer not to use Composition API at all, you can use the `mapActions()` helper to map actions properties as methods in your component:
111155

112156
```js
113157
import { mapActions } from 'pinia'
158+
import { useCounterStore } from '../stores/counterStore'
114159

115160
export default {
116161
methods: {
117162
// gives access to this.increment() inside the component
118163
// same as calling from store.increment()
119-
...mapActions(useStore, ['increment'])
164+
...mapActions(useCounterStore, ['increment'])
120165
// same as above but registers it as this.myOwnName()
121-
...mapActions(useStore, { myOwnName: 'doubleCounter' }),
166+
...mapActions(useCounterStore, { myOwnName: 'doubleCounter' }),
122167
},
123168
}
124169
```

packages/docs/core-concepts/getters.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export const useStore = defineStore('main', {
8484
})
8585
```
8686

87-
8887
## Passing arguments to getters
8988

9089
_Getters_ are just _computed_ properties behind the scenes, so it's not possible to pass any parameters to them. However, you can return a function from the _getter_ to accept any arguments:
@@ -113,7 +112,7 @@ export default {
113112
</script>
114113
115114
<template>
116-
User 2: {{ getUserById(2) }}
115+
<p>User 2: {{ getUserById(2) }}</p>
117116
</template>
118117
```
119118

@@ -165,20 +164,64 @@ export default {
165164
}
166165
```
167166

168-
## Usage with the options API
167+
## Usage with the Options API
168+
169+
For the following examples, you can assume the following store was created:
170+
171+
```js
172+
// Example File Path:
173+
// ./src/stores/counterStore.js
174+
175+
import { defineStore } from 'pinia',
176+
177+
const useCounterStore = defineStore('counterStore', {
178+
state: () => ({
179+
counter: 0
180+
}),
181+
getters: {
182+
doubleCounter() {
183+
return this.counter * 2
184+
}
185+
}
186+
})
187+
```
188+
189+
### With `setup()`
190+
191+
While Composition API is not for everyone, the `setup()` hook can make using Pinia easier to work with in the Options API. No extra map helper functions needed!
192+
193+
```js
194+
import { useCounterStore } from '../stores/counterStore'
195+
196+
export default {
197+
setup() {
198+
const counterStore = useCounterStore()
199+
200+
return { counterStore }
201+
},
202+
computed: {
203+
quadrupleCounter() {
204+
return counterStore.doubleCounter * 2
205+
},
206+
},
207+
}
208+
```
209+
210+
### Without `setup()`
169211

170212
You can use the same `mapState()` function used in the [previous section of state](./state.md#options-api) to map to getters:
171213

172214
```js
173215
import { mapState } from 'pinia'
216+
import { useCounterStore } from '../stores/counterStore'
174217

175218
export default {
176219
computed: {
177220
// gives access to this.doubleCounter inside the component
178221
// same as reading from store.doubleCounter
179-
...mapState(useStore, ['doubleCount'])
222+
...mapState(useCounterStore, ['doubleCount'])
180223
// same as above but registers it as this.myOwnName
181-
...mapState(useStore, {
224+
...mapState(useCounterStore, {
182225
myOwnName: 'doubleCounter',
183226
// you can also write a function that gets access to the store
184227
double: store => store.doubleCount,

packages/docs/core-concepts/state.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,59 @@ const store = useStore()
4747
store.$reset()
4848
```
4949

50-
### Usage with the options API
50+
### Usage with the Options API
5151

52-
If you are not using the composition API, and you are using `computed`, `methods`, ..., you can use the `mapState()` helper to map state properties as readonly computed properties:
52+
For the following examples, you can assume the following store was created:
53+
54+
```js
55+
// Example File Path:
56+
// ./src/stores/counterStore.js
57+
58+
import { defineStore } from 'pinia',
59+
60+
const useCounterStore = defineStore('counterStore', {
61+
state: () => ({
62+
counter: 0
63+
})
64+
})
65+
```
66+
67+
### With `setup()`
68+
69+
While Composition API is not for everyone, the `setup()` hook can make using Pinia easier to work with in the Options API. No extra map helper functions needed!
70+
71+
```js
72+
import { useCounterStore } from '../stores/counterStore'
73+
74+
export default {
75+
setup() {
76+
const counterStore = useCounterStore()
77+
78+
return { counterStore }
79+
},
80+
computed: {
81+
tripleCounter() {
82+
return counterStore.counter * 3
83+
},
84+
},
85+
}
86+
```
87+
88+
### Without `setup()`
89+
90+
If you are not using the Composition API, and you are using `computed`, `methods`, ..., you can use the `mapState()` helper to map state properties as readonly computed properties:
5391

5492
```js
5593
import { mapState } from 'pinia'
94+
import { useCounterStore } from '../stores/counterStore'
5695

5796
export default {
5897
computed: {
5998
// gives access to this.counter inside the component
6099
// same as reading from store.counter
61-
...mapState(useStore, ['counter'])
100+
...mapState(useCounterStore, ['counter'])
62101
// same as above but registers it as this.myOwnName
63-
...mapState(useStore, {
102+
...mapState(useCounterStore, {
64103
myOwnName: 'counter',
65104
// you can also write a function that gets access to the store
66105
double: store => store.counter * 2,
@@ -79,15 +118,16 @@ If you want to be able to write to these state properties (e.g. if you have a fo
79118

80119
```js
81120
import { mapWritableState } from 'pinia'
121+
import { useCounterStore } from '../stores/counterStore'
82122

83123
export default {
84124
computed: {
85125
// gives access to this.counter inside the component and allows setting it
86126
// this.counter++
87127
// same as reading from store.counter
88-
...mapWritableState(useStore, ['counter'])
128+
...mapWritableState(useCounterStore, ['counter'])
89129
// same as above but registers it as this.myOwnName
90-
...mapWritableState(useStore, {
130+
...mapWritableState(useCounterStore, {
91131
myOwnName: 'counter',
92132
}),
93133
},

0 commit comments

Comments
 (0)