From: Eduardo San Martin Morote Date: Thu, 8 Apr 2021 14:25:04 +0000 (+0200) Subject: docs: wip hehe X-Git-Tag: v2.0.0-alpha.11~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2d8b7b6514b2002c405a45d7ba3d1f12cb8a70d1;p=thirdparty%2Fvuejs%2Fpinia.git docs: wip hehe --- diff --git a/docs/introduction.md b/docs/introduction.md index 9996eb0b..2d35876c 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -1,6 +1,6 @@ # Introduction -Pinia [started](https://github.com/posva/pinia/commit/06aeef54e2cad66696063c62829dac74e15fd19e) as an experiment to redesign what a Store for Vue could look like with the [Composition API](https://github.com/vuejs/composition-api) around November 2019. Since then, the initial principles are still the same, but Pinia works for both Vue 2 and Vue 3. The API is the same for both except for _installation_ and _SSR_, and these docs are targeted to Vue 3 **with notes about Vue 2** whenever necessary so it can be used no matter if you are using Vue 2 or Vue 3! +Pinia [started](https://github.com/posva/pinia/commit/06aeef54e2cad66696063c62829dac74e15fd19e) as an experiment to redesign what a Store for Vue could look like with the [Composition API](https://github.com/vuejs/composition-api) around November 2019. Since then, the initial principles are still the same, but Pinia works for both Vue 2 and Vue 3 **and doesn't require you to use the composition API**. The API is the same for both except for _installation_ and _SSR_, and these docs are targeted to Vue 3 **with notes about Vue 2** whenever necessary so it can be used no matter if you are using Vue 2 or Vue 3! ## Basic example @@ -9,7 +9,7 @@ You start by creating a store: ```js import { defineStore } from 'pinia' -export const useCounter = defineStore({ +export const useCounterStore = defineStore({ id: 'counter', state() { return { count: 0 } @@ -24,7 +24,7 @@ And then you _use_ it in a component: ```js export default { setup() { - const counter = useCounter() + const counter = useCounterStore() counter.count++ // with autocompletion ✨ @@ -33,7 +33,36 @@ export default { } ``` - +If you are still not into `setup()` and Composition API, don't worry, Pinia also support a similar set of [_map helpers_ like Vuex](https://vuex.vuejs.org/guide/state.html#the-mapstate-helper). You define stores the same way but then use `mapStores()`, `mapState()`, or `mapActions()`: + +```js +const useCounterStore = defineStore({ + id: 'counter', + state: () => ({ count: 0 }), + getters: { double: state => state.count * 2 }, + actions: { + increment() { + this.count++ + } + } +}) +const useUserStore = defineStore({ id: 'user' }) + +export default { + computed: { + // other computed properties + // ... + // gives access to this.counterStore and this.userStore + ...mapStores(useCounterStore, useUserStore) + // gives read access to this.count and this.double + ...mapState(useCounterStore, ['count', 'double']), + }, + methods: { + // gives access to this.increment() + ...mapActions(useCounterStore, ['increment']), + }, +} +``` ## Why _Pinia_ diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index e9b6de65..51cab55c 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -357,3 +357,72 @@ export function mapActions< return reduced }, {} as MapActionsObjectReturn) } + +/** + * Same as `mapState()` but creates computed setters as well so the state can be + * modified. Differently from `mapState()`, only `state` properties can be + * added. + * + * @param useStore - store to map from + * @param keyMapper - object of state properties + */ +export function mapWritableState< + Id extends string, + S extends StateTree, + G, + A, + KeyMapper extends Record +>( + useStore: StoreDefinition, + keyMapper: KeyMapper +): MapStateObjectReturn +/** + * Allows using state and getters from one store without using the composition + * API (`setup()`) by generating an object to be spread in the `computed` field + * of a component. + * + * @param useStore - store to map from + * @param keys - array of state properties + */ +export function mapWritableState( + useStore: StoreDefinition, + keys: Array +): MapStateReturn +/** + * Allows using state and getters from one store without using the composition + * API (`setup()`) by generating an object to be spread in the `computed` field + * of a component. + * + * @param useStore - store to map from + * @param keysOrMapper - array or object + */ +export function mapWritableState< + Id extends string, + S extends StateTree, + G, + A, + KeyMapper extends Record +>( + useStore: StoreDefinition, + keysOrMapper: Array | KeyMapper +): MapStateReturn | MapStateObjectReturn { + return Array.isArray(keysOrMapper) + ? keysOrMapper.reduce((reduced, key) => { + reduced[key] = { + get(this: ComponentInstance) { + return getCachedStore(this, useStore)[key] + }, + set() {}, + } + return reduced + }, {} as MapStateReturn) + : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { + reduced[key] = { + get(this: ComponentInstance) { + return getCachedStore(this, useStore)[keysOrMapper[key]] + }, + set() {}, + } + return reduced + }, {} as MapStateObjectReturn) +}