顶层 API - F2
下一篇
组件 - Component
Loading...
F2 提供了一组顶层 API,用于处理 JSX 元素、组件引用和子元素操作。这些 API 从 @antv/f-engine 导出,通过 @antv/f2 重新导出。
/*** 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;
提供了用于处理 this.props.children 的方法集合。
类似 Array.map,遍历子元素并返回新的对象。
Children.map<T = any>(children: T | T[] | null,callback: (child: T | null) => any): any
| 参数 | 类型 | 说明 |
|---|---|---|
children | T | T[] | null | 要遍历的子元素或子元素数组 |
callback | (child: T | null) => any | 对每个子元素执行的回调函数 |
处理后的子元素数组。
复制一个元素并合并新的属性。
Children.cloneElement(element: any, props: any): any
| 参数 | 类型 | 说明 |
|---|---|---|
element | any | 要克隆的元素 |
props | any | 要合并的新属性对象 |
新的元素对象。
将子元素转换为数组形式。
Children.toArray<T = any>(element: T | T[] | null): T[] | null
| 参数 | 类型 | 说明 |
|---|---|---|
element | T | T[] | null | 要转换的子元素或子元素数组 |
子元素数组,如果输入为 null 则返回 null。
比较两个元素是否相等。
Children.compare<T = any>(nextElement: T | T[] | null,lastElement: T | T[] | null,callback: (next: T | T[] | null, last: T | T[] | null) => any): any
| 参数 | 类型 | 说明 |
|---|---|---|
nextElement | T | T[] | null | 新的元素或元素数组 |
lastElement | T | T[] | null | 旧的元素或元素数组 |
callback | (next, last) => any | 比较回调函数,接收新旧两个元素 |
回调函数的执行结果。
提供生成 JSX 元素的方法。createElement(type, props, ...children) 和 jsx(type, props, ...children) 是完全一致的,createElement 是 jsx 的别名。
declare function jsx(type: ElementType,props: any,...children: any[]): JSX.Element;
| 参数 | 类型 | 说明 |
|---|---|---|
type | ElementType | 元素类型(组件构造函数或标签名) |
props | any | 元素属性对象 |
children | any[] | 子元素列表(可变参数) |
JSX 元素对象。
提供了创建引用(ref)的方法。返回一个包含 current 属性的对象,用于访问组件实例或 DOM 元素。
declare function createRef<T = any>(): {current: T;};
包含 current 属性的对象,初始值为 null。
import { Children } from '@antv/f2';function ParentComponent({ children }) {return (<group>{Children.map(children, (child) => {// 对每个子元素进行处理return child})}</group>)}
import { Children } from '@antv/f2';function enhanceChildren(children) {return Children.map(children, (child) => {// 为每个子元素添加额外的属性return Children.cloneElement(child, {extraProp: 'value',onClick: handleClick})})}
import { Children } from '@antv/f2';function analyzeChildren(children) {// 将子元素转换为数组以便进行数组操作const childrenArray = Children.toArray(children)const count = childrenArray.lengthreturn <text>共有 {count} 个子元素</text>}
import { Chart, createRef } from '@antv/f2';function MyComponent() {const chartRef = createRef()const handleClick = () => {// 通过 ref 访问图表实例const 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';// 动态创建图表元素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 : 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 的 Children API 与 React 的类似,但有以下差异:
Children 来自 @antv/f-engine,专门为 Canvas 渲染优化Children.toArray 避免不必要的转换current 属性初始值为 nullcurrent 会被重置为 null源码位置:
@antv/f-engine/src/children@antv/f-engine/src/jsx@antv/f-engine/src/createRefpackages/f2/src/index.ts