API Reference
Detailed documentation of React JSONR's API
Core Functions
renderNode
Renders a JSON node to a React element.
Parameters:
node
: The JSON node to renderregistry
: A mapping of component types to React componentscontext
(optional): Additional context, including event handlers
Returns:
- A React element (or null if the component type is not found in the registry)
transformJsonTree
Transforms a JSON tree using the provided visitors/plugins.
Parameters:
root
: The root JSON node to transformvisitors
: An array of transformation pluginsoptions
(optional): Configuration options for the transformationorder
: Traversal order (default: 'depthFirstPre')clone
: Whether to clone the input object (default: true)
Returns:
- A promise that resolves to the transformed JSON tree
traverseJsonTree
Generator function that traverses a JSON tree and yields each node, allowing for interactive node inspection and modification.
Parameters:
root
: The root JSON node to traverseoptions
(optional): Configuration options for the traversalorder
: Traversal order (default: 'depthFirstPre')clone
: Whether to clone the input object (default: false)nodeTypes
: Array of node types to yield (default: all types)
Returns:
- A generator that yields each node along with its context during traversal
Example:
Types
JsonNode
Represents a JSON node that can be rendered to a React component.
Properties:
type
: The component type name (must exist in the registry)props
(optional): Component propschildren
(optional): Child nodeskey
(optional): React key for reconciliationid
(optional): Identifier for tracking or reference
ComponentRegistry
A mapping of component types to React components.
The keys are the type names used in JSON, and the values are either React components or strings (for native HTML elements).
You can create a registry using the createRegistry
function, which automatically includes all HTML elements and special components like Fragment and Portal:
TransformVisitor
Plugin interface for transforming the JSON tree.
Methods:
enter
: Called when a node is first encountered (before processing children)exit
: Called after all children have been processed
TransformContext
Context passed to transform visitors.
Properties:
depth
: Current depth in the treeparent
: Parent node (null for the root)index
: Index of this node in the parent's children arrayskipChildren()
: Function to skip traversing this node's children
TransformOptions
Options for transform operations.
Properties:
order
(optional): Traversal order for the transformation (default: 'depthFirstPre')clone
(optional): Whether to clone the input before transforming (default: true)
TraverseOptions
Options for traversal using the generator.
Properties:
- Inherits all properties from TransformOptions
nodeTypes
(optional): Types of nodes to yield (empty array or undefined = all types)clone
(optional): Whether to clone the input before traversing (default: false)
RenderContext
Context for renderNode.
Properties:
eventHandlers
(optional): Functions that can be referenced by string names in JSON