logo

F2

  • 教程
  • 组件API
  • 图表示例
  • 所有产品antv logo arrow
  • 5.10.0
  • 顶层 API - F2
  • 组件 - Component
  • 画布 - Canvas
  • 时间轴 - Timeline
  • 图表 - Chart
    • 图表 - Chart
    • 几何标记 - Geometry
    • 线 - Line
    • 区间 - Interval
    • 点 - Point
    • 面积 - Area
    • K 线图 - Candlestick
    • 坐标轴 - Axis
    • 图例 - Legend
    • 提示 - tooltip
    • 饼图标签 - PieLabel
    • 标注 - Guide
      • 标签标注 - TagGuide
      • 点标注 - PointGuide
      • 辅助线标注 - LineGuide
      • 矩形标注 - RectGuide
      • 图片标注 - ImageGuide
      • 文本标注 - TextGuide
      • 标注 - Guide
    • 滚动条 - ScrollBar
    • 放大镜 - Magnifier
  • 仪表盘 - Gauge
  • 旭日图 - Sunburst
  • 矩形树图 - Treemap

顶层 API - F2

下一篇
组件 - Component

资源

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库
WeaveFox-前端智能研发

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会
weavefoxWeaveFox-智能研发技术社区

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
weavefoxWeaveFox-前端智能研发
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

F2 提供了一组顶层 API,用于处理 JSX 元素、组件引用和子元素操作。这些 API 从 @antv/f-engine 导出,通过 @antv/f2 重新导出。

TypeScript 类型定义

/**
* Children 工具对象,用于处理子元素
*/
interface Children {
/**
* 遍历子元素数组
* @param children - 子元素或子元素数组
* @param callback - 回调函数,接收每个子元素
* @returns 处理后的子元素数组
*/
map<T = any>(
children: T | T[] | null,
callback: (child: T | null) => any
): any;
/**
* 克隆并返回新的 React 元素
* @param element - 要克隆的元素
* @param props - 要合并的新属性
* @returns 新的元素
*/
cloneElement(element: any, props: any): any;
/**
* 将子元素转换为数组
* @param element - 子元素或子元素数组
* @returns 子元素数组
*/
toArray<T = any>(element: T | T[] | null): T[] | null;
/**
* 比较两个元素
* @param nextElement - 新元素
* @param lastElement - 旧元素
* @param callback - 比较回调
*/
compare<T = any>(
nextElement: T | T[] | null,
lastElement: T | T[] | null,
callback: (next: T | T[] | null, last: T | T[] | null) => any
): any;
}
/**
* 创建元素引用
* @param T - 引用对象的类型
* @returns 包含 current 属性的引用对象
*/
declare function createRef<T = any>(): {
current: T;
};
/**
* 创建 JSX 元素
* @param type - 元素类型(组件构造函数或标签名)
* @param props - 元素属性
* @param children - 子元素
* @returns JSX 元素
*/
declare function jsx(
type: ElementType,
props: any,
...children: any[]
): JSX.Element;
/**
* createElement 是 jsx 的别名,两者功能完全一致
*/
declare function createElement(
type: ElementType,
props: any,
...children: any[]
): JSX.Element;

相关组件

  • Component:组件基类
  • Canvas:画布组件
  • Timeline:时间轴组件
  • Chart:图表组件

Children

提供了用于处理 this.props.children 的方法集合。

Children.map(children, callback)

类似 Array.map,遍历子元素并返回新的对象。

类型定义

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

参数说明

参数类型说明
childrenT | T[] | null要遍历的子元素或子元素数组
callback(child: T | null) => any对每个子元素执行的回调函数

返回值

处理后的子元素数组。


Children.cloneElement(child, props)

复制一个元素并合并新的属性。

类型定义

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

参数说明

参数类型说明
elementany要克隆的元素
propsany要合并的新属性对象

返回值

新的元素对象。


Children.toArray(children)

将子元素转换为数组形式。

类型定义

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

参数说明

参数类型说明
elementT | T[] | null要转换的子元素或子元素数组

返回值

子元素数组,如果输入为 null 则返回 null。


Children.compare(nextElement, lastElement, callback)

比较两个元素是否相等。

类型定义

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

参数说明

参数类型说明
nextElementT | T[] | null新的元素或元素数组
lastElementT | T[] | null旧的元素或元素数组
callback(next, last) => any比较回调函数,接收新旧两个元素

返回值

回调函数的执行结果。


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

提供生成 JSX 元素的方法。createElement(type, props, ...children) 和 jsx(type, props, ...children) 是完全一致的,createElement 是 jsx 的别名。

类型定义

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

参数说明

参数类型说明
typeElementType元素类型(组件构造函数或标签名)
propsany元素属性对象
childrenany[]子元素列表(可变参数)

返回值

JSX 元素对象。


createRef()

提供了创建引用(ref)的方法。返回一个包含 current 属性的对象,用于访问组件实例或 DOM 元素。

类型定义

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

返回值

包含 current 属性的对象,初始值为 null。


用法示例

使用 Children.map 遍历子元素

import { Children } from '@antv/f2';
function ParentComponent({ children }) {
return (
<group>
{Children.map(children, (child) => {
// 对每个子元素进行处理
return child
})}
</group>
)
}

使用 Children.cloneElement 修改子元素属性

import { Children } from '@antv/f2';
function enhanceChildren(children) {
return Children.map(children, (child) => {
// 为每个子元素添加额外的属性
return Children.cloneElement(child, {
extraProp: 'value',
onClick: handleClick
})
})
}

使用 Children.toArray 转换子元素

import { Children } from '@antv/f2';
function analyzeChildren(children) {
// 将子元素转换为数组以便进行数组操作
const childrenArray = Children.toArray(children)
const count = childrenArray.length
return <text>共有 {count} 个子元素</text>
}

使用 createRef 创建组件引用

import { Chart, createRef } from '@antv/f2';
function MyComponent() {
const chartRef = createRef()
const handleClick = () => {
// 通过 ref 访问图表实例
const chart = chartRef.current
console.log(chart.getLayout())
}
return (
<Chart ref={chartRef} data={data}>
<Interval x="genre" y="sold" />
</Chart>
)
}

使用 createElement 动态创建元素

import { createElement, Chart, Interval } from '@antv/f2';
// 动态创建图表元素
const chartElement = createElement(
Chart,
{ data: myData },
createElement(Interval, { x: 'genre', y: 'sold' })
)
// 在渲染中使用
<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 : Interval
return createElement(
Chart,
{ data },
createElement(Geometry, geometryProps)
)
}

使用 Children.compare 比较元素

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
}

注意事项

Children 方法与 React 的差异

F2 的 Children API 与 React 的类似,但有以下差异:

  1. 实现来源:F2 的 Children 来自 @antv/f-engine,专门为 Canvas 渲染优化
  2. 性能考虑:在 Canvas 环境下,建议合理使用 Children.toArray 避免不必要的转换

createRef 使用建议

  1. 初始值:创建的 ref 对象 current 属性初始值为 null
  2. 赋值时机:ref 会在组件挂载后被自动赋值
  3. 清理:组件卸载时,current 会被重置为 null

createElement / jsx 选择

  • jsx:推荐使用,与 JSX 转换器生成的代码一致
  • createElement:jsx 的别名,主要用于兼容旧代码

源码位置:

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