Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix g-react re-render #1757

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading