Working with contexts
This page covers the practical aspects of creating and using contexts in Nordcraft applications. Contexts enable direct communication between ancestor and descendant components without going through intermediate components.
Create a context provider
To create a context provider, you need to expose either formulas or workflows from a component.
Expose a formula in context
- 1In the component's data panel, navigate to the Formulas section and create a new formula or select an existing one
- 2Check the Expose in context checkbox in the formula-editor
The formula will now be available to all descendant components through context.
Expose a workflow in context
- 1In the component's data panel, navigate to the Workflows section and create a new workflow or select an existing one
- 2Check the Expose context checkbox next to the workflow name in the details panel
The workflow can now be triggered directly by any descendant component.
To share a variable's value through context, create a formula that returns the variable and expose it. For variable modification, create and expose a workflow that updates the variable.
Using contexts in components
Components can only access contexts from ancestors in the component tree. The context provider must be higher in the hierarchy than the component using the context.
For a component to use context from an ancestor:
- 1In the selected component, navigate to the Contexts section in the data panel and click the + button to add a context reference
- 2Select the ancestor component that provides the context (Only components that expose context will appear in the list)
- 3Check the specific formulas and workflows you need to access
Once added, the selected formulas and workflows become available in the component:
- Context formulas can be used in any formula editor within the component
- Context workflows can be triggered from any workflow or event handler within the component
When multiple ancestor components provide the same context, the closest ancestor takes precedence.
Example
The following example shows how to set up and use contexts with a simple counter implementation. One counter demonstrates a provider that exposes count and increment through context, while the other shows the same counter built with attribute passing for comparison.