Skip to content

Commit

Permalink
docs(createGlobalState): add an example without persistence (#1991) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
plylrnsdy committed Aug 23, 2022
1 parent 78477de commit 35340b9
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/shared/createGlobalState/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ Keep states in the global scope to be reusable across Vue instances.

## Usage

### Without Persistence (Sotre in Memory)

```js
// store.js
import { ref } from 'vue'
import { createGlobalState } from '@vueuse/core'

export const useGlobalState = createGlobalState(
() => {
const count = ref(0)
return { count }
}
)
```

A bigger example:

```js
// store.js
import { computed, ref } from 'vue'
import { createGlobalState } from '@vueuse/core'

export const useGlobalState = createGlobalState(
() => {
// state
const count = ref(initialValue)

// getters
const doubleCount = computed(() => count.value * 2)

// actions
function increment() {
count.value++
}

return { count, doubleCount, increase }
}
)
```


### With Persistence

Store in `localStorage` with `useStorage()`:

```js
// store.js
import { createGlobalState, useStorage } from '@vueuse/core'
Expand Down

0 comments on commit 35340b9

Please sign in to comment.