logo

F2

  • Manual
  • Component Documentation
  • Examples
  • Productsantv logo arrow
  • 5.10.0
  • Top-Level API - F2
  • Chart
    • Guide

Top-Level API - F2

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library
WeaveFox-AI Coding Assistant

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference
weavefoxWeaveFox-AI Developer Community

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
weavefoxWeaveFox-AI Coding Assistant
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

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.

TypeScript Type Definitions

/**
* 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;

Related Components

  • Component: Component base class
  • Canvas: Canvas component
  • Timeline: Timeline component
  • Chart: Chart component

Children

Provides a collection of methods for handling this.props.children.

Children.map(children, callback)

Similar to Array.map, iterates over children and returns a new array.

Type Definition

Children.map<T = any>(
children: T | T[] | null,
callback: (child: T | null) => any
): any

Parameters

ParameterTypeDescription
childrenT | T[] | nullChild element or array of child elements to iterate
callback(child: T | null) => anyCallback function to execute on each child

Returns

Processed children array.


Children.cloneElement(child, props)

Clone an element and merge new props.

Type Definition

Children.cloneElement(element: any, props: any): any

Parameters

ParameterTypeDescription
elementanyElement to clone
propsanyNew props object to merge

Returns

New element object.


Children.toArray(children)

Convert children to array format.

Type Definition

Children.toArray<T = any>(element: T | T[] | null): T[] | null

Parameters

ParameterTypeDescription
elementT | T[] | nullChild element or array of child elements to convert

Returns

Array of child elements, or null if input is null.


Children.compare(nextElement, lastElement, callback)

Compare if two elements are equal.

Type Definition

Children.compare<T = any>(
nextElement: T | T[] | null,
lastElement: T | T[] | null,
callback: (next: T | T[] | null, last: T | T[] | null) => any
): any

Parameters

ParameterTypeDescription
nextElementT | T[] | nullNew element or array of elements
lastElementT | T[] | nullOld element or array of elements
callback(next, last) => anyComparison callback function receiving both new and old elements

Returns

The result of the callback function execution.


createElement / jsx(type, props, ...children)

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.

Type Definition

declare function jsx(
type: ElementType,
props: any,
...children: any[]
): JSX.Element;

Parameters

ParameterTypeDescription
typeElementTypeElement type (component constructor or tag name)
propsanyElement properties object
childrenany[]List of child elements (variadic)

Returns

JSX element object.


createRef()

Provides a method to create a reference (ref). Returns an object with a current property for accessing component instances or DOM elements.

Type Definition

declare function createRef<T = any>(): {
current: T;
};

Returns

Object with current property, initially null.


Usage Examples

Using Children.map to iterate over children

import { Children } from '@antv/f2';
function ParentComponent({ children }) {
return (
<group>
{Children.map(children, (child) => {
// Process each child element
return child
})}
</group>
)
}

Using Children.cloneElement to modify child props

import { Children } from '@antv/f2';
function enhanceChildren(children) {
return Children.map(children, (child) => {
// Add extra props to each child
return Children.cloneElement(child, {
extraProp: 'value',
onClick: handleClick
})
})
}

Using Children.toArray to convert children

import { Children } from '@antv/f2';
function analyzeChildren(children) {
// Convert children to array for array operations
const childrenArray = Children.toArray(children)
const count = childrenArray.length
return <text>Total {count} children</text>
}

Using createRef to create component reference

import { Chart, createRef } from '@antv/f2';
function MyComponent() {
const chartRef = createRef()
const handleClick = () => {
// Access chart instance via ref
const chart = chartRef.current
console.log(chart.getLayout())
}
return (
<Chart ref={chartRef} data={data}>
<Interval x="genre" y="sold" />
</Chart>
)
}

Using createElement to dynamically create elements

import { createElement, Chart, Interval } from '@antv/f2';
// Dynamically create chart element
const chartElement = createElement(
Chart,
{ data: myData },
createElement(Interval, { x: 'genre', y: 'sold' })
)
// Use in render
<Canvas context={context}>
{chartElement}
</Canvas>

Dynamically generate chart configuration

import { createElement, Chart, Interval, Line } from '@antv/f2';
function createChart(type, data) {
const geometryProps = {
x: 'date',
y: 'value'
}
const Geometry = type === 'line' ? Line : Interval
return createElement(
Chart,
{ data },
createElement(Geometry, geometryProps)
)
}

Using Children.compare to compare elements

import { Children } from '@antv/f2';
function shouldComponentUpdate(nextProps) {
let shouldUpdate = false
Children.compare(
nextProps.children,
this.props.children,
(next, last) => {
if (next !== last) {
shouldUpdate = true
}
}
)
return shouldUpdate
}

Notes

Differences between Children methods and React

F2's Children API is similar to React's, but with these differences:

  1. Implementation source: F2's Children comes from @antv/f-engine, optimized for Canvas rendering
  2. Performance considerations: In Canvas environment, use Children.toArray judiciously to avoid unnecessary conversions

createRef usage recommendations

  1. Initial value: The created ref object's current property is initially null
  2. Assignment timing: ref will be automatically assigned after component mounts
  3. Cleanup: When component unmounts, current will be reset to null

createElement / jsx choice

  • jsx: Recommended, consistent with JSX transformer generated code
  • createElement: Alias for jsx, mainly for compatibility with legacy code

Source locations:

  • Children: @antv/f-engine/src/children
  • jsx/createElement: @antv/f-engine/src/jsx
  • createRef: @antv/f-engine/src/createRef
  • Exports: packages/f2/src/index.ts