Loading...
F2 provides a set of top-level APIs for handling JSX elements, component references, and children manipulation. These APIs are re-exported from @antv/f-engine through @antv/f2.
/*** Children utility object for handling children elements*/interface Children {/*** Map over children array* @param children - Child element or array of child elements* @param callback - Callback function receiving each child* @returns Processed children array*/map<T = any>(children: T | T[] | null,callback: (child: T | null) => any): any;/*** Clone and return a new React Element* @param element - Element to clone* @param props - New props to merge* @returns New element*/cloneElement(element: any, props: any): any;/*** Convert children to an array* @param element - Child element or array of child elements* @returns Array of child elements*/toArray<T = any>(element: T | T[] | null): T[] | null;/*** Compare two elements* @param nextElement - New element* @param lastElement - Old element* @param callback - Comparison callback*/compare<T = any>(nextElement: T | T[] | null,lastElement: T | T[] | null,callback: (next: T | T[] | null, last: T | T[] | null) => any): any;}/*** Create an element reference* @param T - Type of the referenced object* @returns Reference object with current property*/declare function createRef<T = any>(): {current: T;};/*** Create JSX element* @param type - Element type (component constructor or tag name)* @param props - Element properties* @param children - Child elements* @returns JSX element*/declare function jsx(type: ElementType,props: any,...children: any[]): JSX.Element;/*** createElement is an alias for jsx, both are functionally identical*/declare function createElement(type: ElementType,props: any,...children: any[]): JSX.Element;
Provides a collection of methods for handling this.props.children.
Similar to Array.map, iterates over children and returns a new array.
Children.map<T = any>(children: T | T[] | null,callback: (child: T | null) => any): any
| Parameter | Type | Description |
|---|---|---|
children | T | T[] | null | Child element or array of child elements to iterate |
callback | (child: T | null) => any | Callback function to execute on each child |
Processed children array.
Clone an element and merge new props.
Children.cloneElement(element: any, props: any): any
| Parameter | Type | Description |
|---|---|---|
element | any | Element to clone |
props | any | New props object to merge |
New element object.
Convert children to array format.
Children.toArray<T = any>(element: T | T[] | null): T[] | null
| Parameter | Type | Description |
|---|---|---|
element | T | T[] | null | Child element or array of child elements to convert |
Array of child elements, or null if input is null.
Compare if two elements are equal.
Children.compare<T = any>(nextElement: T | T[] | null,lastElement: T | T[] | null,callback: (next: T | T[] | null, last: T | T[] | null) => any): any
| Parameter | Type | Description |
|---|---|---|
nextElement | T | T[] | null | New element or array of elements |
lastElement | T | T[] | null | Old element or array of elements |
callback | (next, last) => any | Comparison callback function receiving both new and old elements |
The result of the callback function execution.
Provides methods to create JSX elements. createElement(type, props, ...children) and jsx(type, props, ...children) are functionally identical, with createElement being an alias for jsx.
declare function jsx(type: ElementType,props: any,...children: any[]): JSX.Element;
| Parameter | Type | Description |
|---|---|---|
type | ElementType | Element type (component constructor or tag name) |
props | any | Element properties object |
children | any[] | List of child elements (variadic) |
JSX element object.
Provides a method to create a reference (ref). Returns an object with a current property for accessing component instances or DOM elements.
declare function createRef<T = any>(): {current: T;};
Object with current property, initially null.
import { Children } from '@antv/f2';function ParentComponent({ children }) {return (<group>{Children.map(children, (child) => {// Process each child elementreturn child})}</group>)}
import { Children } from '@antv/f2';function enhanceChildren(children) {return Children.map(children, (child) => {// Add extra props to each childreturn Children.cloneElement(child, {extraProp: 'value',onClick: handleClick})})}
import { Children } from '@antv/f2';function analyzeChildren(children) {// Convert children to array for array operationsconst childrenArray = Children.toArray(children)const count = childrenArray.lengthreturn <text>Total {count} children</text>}
import { Chart, createRef } from '@antv/f2';function MyComponent() {const chartRef = createRef()const handleClick = () => {// Access chart instance via refconst chart = chartRef.currentconsole.log(chart.getLayout())}return (<Chart ref={chartRef} data={data}><Interval x="genre" y="sold" /></Chart>)}
import { createElement, Chart, Interval } from '@antv/f2';// Dynamically create chart elementconst chartElement = createElement(Chart,{ data: myData },createElement(Interval, { x: 'genre', y: 'sold' }))// Use in render<Canvas context={context}>{chartElement}</Canvas>
import { createElement, Chart, Interval, Line } from '@antv/f2';function createChart(type, data) {const geometryProps = {x: 'date',y: 'value'}const Geometry = type === 'line' ? Line : Intervalreturn createElement(Chart,{ data },createElement(Geometry, geometryProps))}
import { Children } from '@antv/f2';function shouldComponentUpdate(nextProps) {let shouldUpdate = falseChildren.compare(nextProps.children,this.props.children,(next, last) => {if (next !== last) {shouldUpdate = true}})return shouldUpdate}
F2's Children API is similar to React's, but with these differences:
Children comes from @antv/f-engine, optimized for Canvas renderingChildren.toArray judiciously to avoid unnecessary conversionscurrent property is initially nullcurrent will be reset to nullSource locations:
@antv/f-engine/src/children@antv/f-engine/src/jsx@antv/f-engine/src/createRefpackages/f2/src/index.ts