chore(docs): add state injection section to useVueFlow docs

This commit is contained in:
braks
2024-06-13 11:17:24 +02:00
parent 6345176b25
commit fe55415b2d
3 changed files with 40 additions and 11 deletions
+25
View File
@@ -35,6 +35,7 @@ onInit((instance) => {
} }
}) })
</script> </script>
<template> <template>
<VueFlow :nodes="nodes" :edges="edges" /> <VueFlow :nodes="nodes" :edges="edges" />
</template> </template>
@@ -43,6 +44,30 @@ onInit((instance) => {
`useVueFlow` exposes the whole internal state, including the nodes and edges. `useVueFlow` exposes the whole internal state, including the nodes and edges.
The values are reactive, meaning changing the values returned from `useVueFlow` will trigger changes in the graph. The values are reactive, meaning changing the values returned from `useVueFlow` will trigger changes in the graph.
### State creation and injection
The `useVueFlow` composable creates, on first call, a new instance of the `VueFlowStore` and injects it into the Vue component tree.
This allows you to access the store from any child component using the `useVueFlow` composable.
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.
You can think of it as a sort of `<VueFlowProvider>` wrapper that is automatically injected into the component tree.
You can read more about this in the [State section of the guide](/guide/vue-flow/state).
#### Enforcing a specific state instance
If necessary, you can enforce the use of a specific state instance by passing an `id` to the `useVueFlow` composable.
```ts
import { useVueFlow } from '@vue-flow/core'
const { onInit } = useVueFlow({ id: 'my-flow-instance' })
onInit((instance) => {
// `instance` is the same type as the return of `useVueFlow` (VueFlowStore)
})
```
## [useHandleConnections](/typedocs/functions/useHandleConnections) ## [useHandleConnections](/typedocs/functions/useHandleConnections)
`useHandleConnections` provides you with an array of connections that are connected to the node you pass to it. `useHandleConnections` provides you with an array of connections that are connected to the node you pass to it.
+14 -10
View File
@@ -136,7 +136,7 @@ Sidebar or Toolbar.
<script setup> <script setup>
import { ref } from 'vue' import { ref } from 'vue'
import { VueFlow, useVueFlow } from '@vue-flow/core' import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
const initialNodes = ref([ const initialNodes = ref([
{ {
@@ -167,10 +167,12 @@ function onAddNodes() {
</script> </script>
<template> <template>
<VueFlow :nodes="initialNodes" /> <VueFlow :nodes="initialNodes">
<Panel>
<button type="button" @click="onAddNode">Add a node</button> <button type="button" @click="onAddNode">Add a node</button>
<button type="button" @click="onAddNodes">Add multiple nodes</button> <button type="button" @click="onAddNodes">Add multiple nodes</button>
</Panel>
</VueFlow>
</template> </template>
``` ```
@@ -178,7 +180,7 @@ function onAddNodes() {
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { ref } from 'vue'
import type { Node } from '@vue-flow/core' import type { Node } from '@vue-flow/core'
import { VueFlow, useVueFlow } from '@vue-flow/core' import { Panel, VueFlow, useVueFlow } from '@vue-flow/core'
const initialNodes = ref<Node[]>([ const initialNodes = ref<Node[]>([
{ {
@@ -213,10 +215,12 @@ function onAddNodes() {
</script> </script>
<template> <template>
<VueFlow :nodes="initialNodes" /> <VueFlow :nodes="initialNodes">
<Panel>
<button type="button" @click="onAddNode()">Add a node</button> <button type="button" @click="onAddNode">Add a node</button>
<button type="button" @click="onAddNodes()">Add multiple nodes</button> <button type="button" @click="onAddNodes">Add multiple nodes</button>
</Panel>
</VueFlow>
</template> </template>
``` ```
+1 -1
View File
@@ -1,6 +1,6 @@
# State # State
## ## Introduction
Under the hood Vue Flow uses [Provide/Inject](https://v3.vuejs.org/guide/component-provide-inject) Under the hood Vue Flow uses [Provide/Inject](https://v3.vuejs.org/guide/component-provide-inject)
to pass around it's state between components. to pass around it's state between components.