Vue

Install

1
npm install overmind overmind-vue
Copied!
There are three approaches to connecting Overmind to Vue.

Hooks (experimental)

JavaScript
TypeScript
1
// overmind/index.js
2
import {
3
createStateHook,
4
createActionsHook,
5
createEffectsHook,
6
createReactionHook
7
} from 'overmind-vue/vu3'
8
9
export const config = {
10
state: {
11
foo: 'bar'
12
},
13
actions: {
14
onClick() {}
15
},
16
effects: {}
17
}
18
19
export const hooks = {
20
state: createStateHook(),
21
actions: createActionsHook(),
22
effects: createEffectsHook(),
23
reaction: createReactionHook()
24
}
25
26
// index.js
27
import { createOvermind } from 'overmind'
28
import { withOvermind } from 'overmind-vue/vu3'
29
import { config } from './overmind'
30
import App from './App.vue'
31
32
const overmind = createOvermind(config)
33
34
createApp(withOvermind(overmind, App)).mount('#app')
35
36
...
37
38
// components/SomeComponent.vue
39
<template>
40
<div @click="actions.onClick">
41
{{ state.foo }}
42
</div>
43
</template>
44
<script>
45
import { hooks } from '../overmind'
46
47
export default {
48
setup() {
49
const state = hooks.state()
50
const actions = hooks.actions()
51
52
return { state, actions }
53
}
54
}
55
</script>
Copied!
1
// overmind/index.ts
2
import { IContext } from 'overmind'
3
4
import {
5
createStateHook,
6
createActionsHook,
7
createEffectsHook,
8
createReactionHook
9
} from 'overmind-vue/vu3'
10
import { state } from './state'
11
import * as actions from './actions'
12
13
export const config = {
14
state,
15
actions
16
}
17
18
export type Context = IContext<typeof config>
19
20
export const hooks = {
21
state: createStateHook<Context>(),
22
actions: createActionsHook<Context>(),
23
effects: createEffectsHook<Context>(),
24
reaction: createReactionHook<Context>()
25
}
26
27
// index.ts
28
import { createOvermind } from 'overmind'
29
import { withOvermind } from 'overmind-vue/vu3'
30
import { config } from './overmind'
31
import App from './App.vue'
32
33
const overmind = createOvermind(config)
34
35
createApp(withOvermind(overmind, App)).mount('#app')
36
37
...
38
39
// components/SomeComponent.vue
40
<template>
41
<div @click="actions.onClick">
42
{{ state.foo }}
43
</div>
44
</template>
45
<script>
46
import { hooks } from '../overmind'
47
48
export default {
49
setup() {
50
const state = hooks.state()
51
const actions = hooks.actions()
52
53
return { state, actions }
54
}
55
}
56
</script>
Copied!

Accessing state in setup

You have to point to .value to access state from the hook with setup.
components/SomeComponent.vue
1
<script>
2
import { hooks } from '../overmind'
3
4
export default {
5
setup() {
6
const state = hooks.state()
7
const actions = hooks.actions()
8
9
return {
10
foo: state.value.foo,
11
actions
12
}
13
14
// JSX
15
return () => (
16
<div onClick={actions.onClick}>{state.value.foo}</div>
17
)
18
}
19
}
20
</script>
Copied!

Scoped tracking

You can scope the tracking to specific state. In this example we only track changes to the properties accessed on the item within the template. Any changes to the items state itself does not affect this component, like adding/removing items.
components/SomeComponent.vue
1
<template>
2
<div @click="actions.onClick">
3
{{ item.title }}
4
</div>
5
</template>
6
<script>
7
import { hooks } from '../overmind'
8
9
export default {
10
props: ['id'],
11
setup() {
12
const item = hooks.state(state => state.items[this.id])
13
const actions = hooks.actions()
14
15
return { item, actions }
16
}
17
}
18
</script>
Copied!

Plugin

Vue has a plugin system that allows us to expose Overmind to all components. This allows minimum configuration and you just use state etc. from any component.
overmind/index.js
index.js
components/SomeComponent.vue
1
export const overmind = {
2
state: {
3
foo: 'bar'
4
},
5
actions: {
6
onClick() {}
7
}
8
}
Copied!
1
import Vue from 'vue/dist/vue'
2
import { createOvermind } from 'overmind'
3
import { createPlugin } from 'overmind-vue'
4
import { config } from './overmind'
5
6
const overmind = createOvermind(config)
7
const OvermindPlugin = createPlugin(overmind)
8
9
Vue.use(OvermindPlugin)
10
11
...
Copied!
1
<template>
2
<div @click="actions.onClick">
3
{{ state.foo }}
4
</div>
5
</template>
Copied!
If you rather want to expose state, actions and effects differently you can configure that.
index.js
components/SomeComponent.vue
1
import Vue from 'vue/dist/vue'
2
import { createOvermind } from 'overmind'
3
import { createPlugin } from 'overmind-vue'
4
import { config } from './overmind'
5
6
const overmind = createOvermind(config)
7
const OvermindPlugin = createPlugin(overmind)
8
9
Vue.use(OvermindPlugin, ({ state, actions, effects }) => ({
10
admin: state.admin,
11
posts: state.posts,
12
actions,
13
effects
14
}))
15
16
...
Copied!
1
<template>
2
<div @click="actions.onClick">
3
{{ admin.foo }} {{ posts.foo }}
4
</div>
5
</template>
Copied!

Rendering

Any state accessed in the component will cause the component to render when a mutation occurs on that state. Overmind actually uses the same approach to change detection as Vue itself. When using the plugin any component can access any state, though the only overhead that is added to the application is an instance of a “tracking tree” per component. This might sound scary, but it is a tiny little object that adds a callback function to Overmind as long as the component lives. These tracking trees are even reused as components unmount.

Pass state as props

If you pass anything from the state to a child component it will just work out of the box. The child component will “rescope” the property to its own tracking tree. This ensures that the property you passed is tracked within that component.
components/Todo.vue
components/Todos.vue
1
<template>
2
<li>{{ todo.title }}</li>
3
</template>
4
<script>
5
export default {
6
name: 'Todo',
7
props: ["todo"]
8
}
9
</script>
Copied!
1
<template>
2
<ul>
3
<todo-component
4
v-for="post in state.postsList"
5
:todo="todo"
6
:key="todo.id"
7
></todo-component>
8
</ul>
9
</template>
10
<script>
11
import TodoComponent from './Todo'
12
13
export default {
14
name: 'Todo',
15
components: {
16
TodoComponent
17
}
18
}
19
</script>
Copied!

Reactions

To run effects in components based on changes to state you use the reaction function in the lifecycle hooks of Vue.
components/SomeComponent.vue
1
<template>
2
<div @click="overmind.actions.onClick">
3
{{ overmind.state.foo }}
4
</div>
5
</template>
6
<script>
7
import { connect } from '../overmind'
8
9
export default connect({
10
mounted() {
11
this.disposeReaction = this.overmind.reaction(
12
({ currentPage }) => currentPage,
13
() => document.querySelector('#app').scrollTop = 0
14
)
15
},
16
destroyed() {
17
this.disposeReaction()
18
}
19
})
20
</script>
Copied!

Connect

If you want more manual control of what components connect to Overmind you can use the connector.
overmind/index.js
components/SomeComponent.vue
1
import { createOvermind } from 'overmind'
2
import { createConnect } from 'overmind-vue'
3
4
const overmind = createOvermind({
5
state: {},
6
actions: {}
7
})
8
9
export const connect = createConnect(overmind)
Copied!
1
<template>
2
<div @click="overmind.actions.onClick">
3
{{ overmind.state.foo }}
4
</div>
5
</template>
6
<script>
7
import { connect } from '../overmind'
8
9
const Component = {}
10
11
export default connect(Component)
12
</script>
Copied!
You can also expose parts of the configuration on custom properties of the component:
components/SomeComponent.vue
1
<template>
2
<div @click="actions.someAdminAction">
3
{{ state.someAdminState }}
4
</div>
5
</template>
6
<script>
7
import { connect } from '../overmind'
8
9
const Component = {}
10
11
export default connect(({ state, actions, effects }) => ({
12
state: state.admin,
13
actions: actions.admin
14
}), Component)
15
</script>
Copied!
You can now access the admin state and actions directly with state and actions.

Computed

Vue has its own observable concept that differs from Overmind. That means you can not use Overmind state inside a computed and expect the computed cache to be busted when the Overmind state changes. But computeds are really for caching expensive computation, which you will rather do inside Overmind using derived anyways.

Using props

You can combine Overmind state with props to dynamically extract state.
components/SomeComponent.vue
1
<template>
2
<div>
3
{{ title }}
4
</div>
5
</template>
6
<script>
7
export default {
8
name: 'SomeComponent',
9
props: ["id"],
10
data: (self) => ({
11
get title() {
12
return self.state.titles[self.id]
13
}
14
})
15
}
16
</script>
Copied!
Last modified 4mo ago