Plugins
Enhancing JSON with transformation plugins
One of the most powerful features of React JSONR is its plugin system. Plugins allow you to transform and enhance the JSON before it's rendered to React elements.
What Are Plugins?
Plugins are visitors that get called for each node in your JSON tree. They can:
- Modify nodes (change props, add/remove children)
- Add data from external sources
- Validate the JSON structure
- Enforce patterns or add defaults
- Skip subtrees for optimization
Plugin Interface
A plugin implements the TransformVisitor
interface:
Each plugin can have:
- An
enter
method called when a node is first visited (before its children) - An
exit
method called after all children have been processed
Example Plugins
Auto ID Plugin
This simple plugin adds an ID to any element without one:
Data Fetching Plugin
This async plugin fetches user data and updates the JSON:
Using Plugins
To use plugins, pass them as an array to the transformJsonTree
function:
Plugins are applied in the order they're provided, so later plugins will see the changes made by earlier ones.
Traversal Orders
React JSONR supports different traversal strategies:
depthFirstPre
: Processes a node, then its children (default)depthFirstPost
: Processes children first, then the nodebreadthFirst
: Processes all nodes at a given depth before moving deeper
Performance Optimization
For large trees, you can optimize plugin execution by skipping subtrees:
This can significantly improve performance for complex UIs with static sections.