顶层 API - F2

2 min read

顶层 API

Children

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

Children.map(children, callback)

类似 Array.map 遍历并返回新的对象

import { Children } from '@antv/f2';
Children.map(this.props.children, (child) => {
  // TODO
  return child;
});

Children.cloneElement(child, props)

复制一个新元素

import { Children } from '@antv/f2';
Children.map(this.props.children, (child) => {
  return Children.cloneElement(child, { ... });
});

Children.toArray(children)

转换成数组

import { Children } from '@antv/f2';
const childrenArray = Children.toArray(children);

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

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

import { createElement, Chart } from '@antv/f2';
// 创建 chart 元素
const element = createElement(Chart, { data }, [...]);

createRef()

提供了创建 ref 的方法

import { createRef } from '@antv/f2';
// 创建 chart 元素
const ref = createRef(); // 生成 { current: null } 这样的结构