Consuming Variables
In FlowGram, when a node wants to use variables from preceding nodes, it needs to consume those variables.
- We recommend finishing Output Variables first so you know how variables are produced; this guide is the second stop focused on “how to access them.”
- To quickly preview the variable selector experience, try VariableSelector first; continue with the API sections when you need code-level access to variable lists.
- All examples assume the node scope. When private or global scopes show up, refer to Core Concepts – Variables in the Canvas for additional context.
VariableSelector
To make it easier for you to integrate variable selection functionality into your applications, the official materials provide the VariableSelector component.
See documentation: VariableSelector
Getting Accessible Variable Tree
In canvas nodes, we often need to get variables available in the current scope and display them in a tree structure for users to select and operate.
- Just list variables →
useAvailableVariables - Need drill-down for objects/arrays →
ASTMatch+ recursive rendering - Need precise subscriptions → go straight to
scope.available
useAvailableVariables
useAvailableVariables is a lightweight Hook that directly returns an array of variables available in the current scope (VariableDeclaration[]).
Getting Object Type Variable Drill-down
When a variable's type is Object, we often need to be able to "drill down" into its interior to access its properties. The ASTMatch.isObject method can help us determine if a variable type is an object. If it is, we can recursively render its properties.
Each layer of the variable tree is essentially a declaration (BaseVariableField). For objects, properties gives you the next-level declaration array.
Getting Array Type Variable Drill-down
Similar to Object type, when encountering an Array type variable, we also want to display its internal structure. For arrays, we usually care about the type of their elements. ASTMatch.isArray can determine if a variable type is an array. It's worth noting that the element type of an array can be any type, and it might even be another array. Therefore, we need a recursive helper function getTypeChildren to handle this situation.
scope.available
scope.available is one of the cores of the variable system, which can perform more advanced variable retrieval and monitoring actions on variables available within the scope.
- You need to read or validate a variable by
keyPath. - You want to manipulate visibility outside of React hooks (e.g., in plugins or services).
- You need fine-grained subscriptions without refreshing the entire list.
useScopeAvailable
useScopeAvailable can directly return scope.available in React.
- Return Value Different:
useAvailableVariablesdirectly returns an array of variables, whileuseScopeAvailablereturns aScopeAvailableDataobject that includes avariablesproperty and other methods. - Applicable Scenario: When you need to perform more complex operations on variables, such as tracking changes in a single variable through
trackByKeyPath,useScopeAvailableis your best choice.
If you don't want automatic refresh, you can turn it off through the autoRefresh parameter:
getByKeyPath
Through getByKeyPath, you can get a specific variable field (including variables nested in Object or Array) from the accessible variables in the current scope.
getByKeyPath is often used in variable validation, such as:
trackByKeyPath
When you only care about changes to a specific variable field (including variables nested in Object or Array), trackByKeyPath allows you to precisely "subscribe" to updates of that variable without causing component re-renders due to changes in other unrelated variables, thus achieving more refined performance optimization.
Combine it with autoRefresh: false to avoid wide re-renders—only update local state when the tracked variable changes.
Overall Listening API
In addition to trackByKeyPath, ScopeAvailableData also provides a set of event listening APIs for overall variable changes, allowing you to more precisely control the response logic for variable changes.
This is very useful when dealing with complex scenarios that require manual management of subscriptions.
Below we use a table to compare these three core listening APIs in detail:
Let's see how to use these APIs in components through a specific example.
These APIs all return a Disposable object. To avoid memory leaks and unnecessary calculations, you must call its dispose() method in the cleanup function of useEffect to cancel the listening.
Getting Output Variables of Current Scope
useOutputVariables
useOutputVariables can get output variables of the current scope and automatically trigger a refresh when the output variable list or drill-down changes.
useOutputVariables is available in flowgram@0.5.6 and later versions. If you are on an earlier version, you can implement it with the following code:
Other APIs
Getting Current Scope
You can get the current scope through useCurrentScope.
Setting Current Scope
You can set the current scope through ScopeProvider.