logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.26
  • Getting Started
  • Section I - Defining the Scenario
  • Section II - Using the renderer
  • Section III - Adding some interaction
  • Diving Deeper
    • Scene Graph
    • 创造一个“太阳系”
    • 使用相机
    • 使用插件
    • 实现一个简单的动画
    • GPGPU 初体验
    • 进入 3D 世界
    • 使用 Box2D 物理引擎
    • 使用 matter.js 物理引擎
    • 使用 Yoga 布局引擎
    • Takeover D3's rendering
    • Takeover Observable Plot's rendering
    • Choose a renderer
    • Rendering on demand
  • Advanced Topics
    • 性能优化
    • 自定义图形
    • 理解事件传播路径
    • Using react-g
    • Exporting the contents of the canvas
    • Using lite version

Using react-g

Previous
理解事件传播路径
Next
Exporting the contents of the canvas

Resource

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

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

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
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

react-g

react render for @antv/g

Install

npm i @antv/react-g

Usage

react-g provide host-component:

  • Container: Canvas and Group.
  • Shape: Text, Circle, Ellipse, Image, Line, Marker, Path, Polygon and Polyline.

Basic usage

import React, { useState } from 'react';
import { Canvas, Circle } from '@antv/react-g';
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
const renderer = new CanvasRenderer();
const App = () => {
const [size, setSize] = useState(50);
return (
<Canvas width={600} height={400} renderer={renderer}>
<Circle
x={100}
y={200}
r={size}
fill="#1890FF"
stroke="#F04864"
lineWidth={4}
onClick={() => {
setSize(100);
}}
/>
</Canvas>
);
};
export default App;

Use ref to access shape instance

Like react-dom, you can use ref to access the shape instance.

import React, { useState, useRef } from 'react';
import { Canvas, Circle } from '@antv/react-g';
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
const renderer = new CanvasRenderer();
const App = () => {
const circleRef = useRef();
const [size, setSize] = useState(50);
return (
<Canvas width={600} height={400} renderer={renderer}>
<Circle
ref={circleRef}
x={100}
y={200}
r={size}
fill="#1890FF"
stroke="#F04864"
lineWidth={4}
onClick={() => {
setSize(100);
}}
/>
</Canvas>
);
};
export default App;

render react-g component to target g element

  • 将 react-g 组件渲染到任意的 g 实例(Canvas/Group/Shape)中
  • 意味着可以将 react-g 组件渲染到 g2,g6 等其他库中
import React, { useState } from 'react';
import { Canvas as GCanvas } from '@antv/g';
import { Circle, render } from '@antv/react-g';
import { Renderer as CanvasRenderer } from '@antv/g-canvas';
const renderer = new CanvasRenderer();
const CircleComponent = () => {
const [size, setSize] = useState(50);
return (
<Circle
x={100}
y={200}
r={size}
fill="#1890FF"
stroke="#F04864"
lineWidth={4}
onMouseenter={() => {
setSize(100);
}}
onMouseleave={() => {
setSize(50);
}}
/>
);
};
const canvas = new GCanvas({
container: 'container', // DOM 节点id
width: 600,
height: 500,
renderer,
});
// canvas can also be group/shape
render(<CircleComponent />, canvas);