Skip to content

Commit

Permalink
fix: fix g-react re-render (#1757)
Browse files Browse the repository at this point in the history
* fix: render cache target to avoid rerender

* refactor: fix typo of reconciler
  • Loading branch information
Aarebecca authored Aug 22, 2024
1 parent 788245f commit d52a8d8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/react-g/src/canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CanvasConfig } from '@antv/g';
import { Canvas as GCanvas } from '@antv/g';
import React, { forwardRef, useLayoutEffect, useRef } from 'react';
import type { FiberRoot } from 'react-reconciler';
import { reconcilor } from './reconciler';
import { reconciler } from './reconciler';
import { assertRef } from './util';

export interface CanvasProps extends CanvasConfig {
Expand Down Expand Up @@ -52,7 +52,7 @@ export const Canvas = forwardRef<GCanvas, CanvasProps>(
canvasRef.current = canvas;

// @ts-ignore
container.current = reconcilor.createContainer(
container.current = reconciler.createContainer(
canvas as any,
1,
false,
Expand All @@ -61,14 +61,14 @@ export const Canvas = forwardRef<GCanvas, CanvasProps>(

return () => {
// @ts-ignore
reconcilor.updateContainer(null, container.current, null);
reconciler.updateContainer(null, container.current, null);
};
}, []);

useLayoutEffect(() => {
if (container.current) {
// @ts-ignore
reconcilor.updateContainer(children, container.current, null);
reconciler.updateContainer(children, container.current, null);
}
}, [children]);

Expand Down
16 changes: 10 additions & 6 deletions packages/react-g/src/reconciler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Canvas, Element } from '@antv/g';
import React from 'react';
import type { OpaqueHandle } from 'react-reconciler';
import type { OpaqueHandle, OpaqueRoot } from 'react-reconciler';
import ReactReconciler from 'react-reconciler';
import { unstable_now as now } from 'scheduler';
import { bindShapeEvent, updateProps } from './processProps';
Expand All @@ -20,7 +20,7 @@ import type {
UpdatePayload,
} from './types';

export const reconcilor = ReactReconciler<
export const reconciler = ReactReconciler<
Type,
Props,
Container,
Expand Down Expand Up @@ -346,7 +346,7 @@ export const reconcilor = ReactReconciler<
): void {},
});

reconcilor.injectIntoDevTools({
reconciler.injectIntoDevTools({
// findFiberByHostInstance: () => {},
// @ts-ignore
bundleType: process.env.NODE_ENV !== 'production' ? 1 : 0,
Expand All @@ -359,6 +359,8 @@ reconcilor.injectIntoDevTools({
},
});

const TargetContainerWeakMap = new WeakMap<Element | Canvas, OpaqueRoot>();

/**
* render react-g component to target g element
* 将react-g组件渲染到任意的g实例(Canvas/Group/Shape)中
Expand All @@ -372,7 +374,9 @@ export const render = (
target: Element | Canvas,
callback?: (() => void) | null,
) => {
// @ts-ignore
const container = reconcilor.createContainer(target as any, 1, false, null);
reconcilor.updateContainer(component, container, null, callback);
const container =
TargetContainerWeakMap.get(target) ||
reconciler.createContainer(target as any, 1, false, null);
TargetContainerWeakMap.set(target, container);
reconciler.updateContainer(component, container, null, callback);
};
2 changes: 1 addition & 1 deletion site/docs/guide/advanced-topics/use-react-g.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: 使用 React 定义图形
order: 5
---

react 通过 `react-reconcilor` 提供了自定义 render 的能力,`react-g` 便是一个 react 到 g 的 render。
react 通过 `react-reconciler` 提供了自定义 render 的能力,`react-g` 便是一个 react 到 g 的 render。

`react-g` 目前处于实验状态,欢迎试用和反馈。

Expand Down

0 comments on commit d52a8d8

Please sign in to comment.