Skip to content

Commit fe55415

Browse files
committed
chore(docs): add state injection section to useVueFlow docs
1 parent 6345176 commit fe55415

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

docs/src/guide/composables.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ onInit((instance) => {
3535
}
3636
})
3737
</script>
38+
3839
<template>
3940
<VueFlow :nodes="nodes" :edges="edges" />
4041
</template>
@@ -43,6 +44,30 @@ onInit((instance) => {
4344
`useVueFlow` exposes the whole internal state, including the nodes and edges.
4445
The values are reactive, meaning changing the values returned from `useVueFlow` will trigger changes in the graph.
4546

47+
### State creation and injection
48+
49+
The `useVueFlow` composable creates, on first call, a new instance of the `VueFlowStore` and injects it into the Vue component tree.
50+
This allows you to access the store from any child component using the `useVueFlow` composable.
51+
52+
This also means that the *first call* of `useVueFlow` is crucial as it determines the state instance that will be used throughout the component tree.
53+
You can think of it as a sort of `<VueFlowProvider>` wrapper that is automatically injected into the component tree.
54+
55+
You can read more about this in the [State section of the guide](/guide/vue-flow/state).
56+
57+
#### Enforcing a specific state instance
58+
59+
If necessary, you can enforce the use of a specific state instance by passing an `id` to the `useVueFlow` composable.
60+
61+
```ts
62+
import { useVueFlow } from '@vue-flow/core'
63+
64+
const { onInit } = useVueFlow({ id: 'my-flow-instance' })
65+
66+
onInit((instance) => {
67+
// `instance` is the same type as the return of `useVueFlow` (VueFlowStore)
68+
})
69+
```
70+
4671
## [useHandleConnections](/typedocs/functions/useHandleConnections)
4772

4873
`useHandleConnections` provides you with an array of connections that are connected to the node you pass to it.

docs/src/guide/node.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Sidebar or Toolbar.
136136
137137
<script setup>
138138
import { ref } from 'vue'
139-
import { VueFlow, useVueFlow } from '@vue-flow/core'
139+
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
140140
141141
const initialNodes = ref([
142142
{
@@ -167,18 +167,20 @@ function onAddNodes() {
167167
</script>
168168
169169
<template>
170-
<VueFlow :nodes="initialNodes" />
171-
172-
<button type="button" @click="onAddNode">Add a node</button>
173-
<button type="button" @click="onAddNodes">Add multiple nodes</button>
170+
<VueFlow :nodes="initialNodes">
171+
<Panel>
172+
<button type="button" @click="onAddNode">Add a node</button>
173+
<button type="button" @click="onAddNodes">Add multiple nodes</button>
174+
</Panel>
175+
</VueFlow>
174176
</template>
175177
```
176178

177179
```vue [<LogosTypescript />]
178180
<script setup lang="ts">
179181
import { ref } from 'vue'
180182
import type { Node } from '@vue-flow/core'
181-
import { VueFlow, useVueFlow } from '@vue-flow/core'
183+
import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
182184
183185
const initialNodes = ref<Node[]>([
184186
{
@@ -213,10 +215,12 @@ function onAddNodes() {
213215
</script>
214216
215217
<template>
216-
<VueFlow :nodes="initialNodes" />
217-
218-
<button type="button" @click="onAddNode()">Add a node</button>
219-
<button type="button" @click="onAddNodes()">Add multiple nodes</button>
218+
<VueFlow :nodes="initialNodes">
219+
<Panel>
220+
<button type="button" @click="onAddNode">Add a node</button>
221+
<button type="button" @click="onAddNodes">Add multiple nodes</button>
222+
</Panel>
223+
</VueFlow>
220224
</template>
221225
```
222226

docs/src/guide/vue-flow/state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# State
22

3-
##
3+
## Introduction
44

55
Under the hood Vue Flow uses [Provide/Inject](https://v3.vuejs.org/guide/component-provide-inject)
66
to pass around it's state between components.

0 commit comments

Comments
 (0)