antvis/G2

Geometry destroy 之后 render 会报错 "Cannot read properties of undefined (reading 'push')" #4179

yinhangfeng posted onGitHub

version: 4.2.8

报错位置

Uncaught TypeError: Cannot read properties of undefined (reading 'push')
 at push../node_modules/@antv/g-base/esm/abstract/container.js.Container.add (container.js:297:1)
    at push../node_modules/@antv/g-base/esm/abstract/container.js.Container.addShape (container.js:204:1)
    at Object.draw (index.js:91:1)
    at Object.drawShape (base.js:77:1)
    at push../node_modules/@antv/g2/esm/geometry/element/index.js.Element.drawShape (index.js:320:1)
    at push../node_modules/@antv/g2/esm/geometry/element/index.js.Element.draw (index.js:42:1)
    at push../node_modules/@antv/g2/esm/geometry/path.js.Path.updateElements (path.js:63:1)
    at push../node_modules/@antv/g2/esm/geometry/base.js.Geometry.paint (base.js:385:1)
    at push../node_modules/@antv/g2/esm/chart/view.js.View.paintGeometries (view.js:1517:1)
    at push../node_modules/@antv/g2/esm/chart/view.js.View.renderPaintRecursive (view.js:1135:1)
    at push../node_modules/@antv/g2/esm/chart/view.js.View.paint (view.js:1016:1)
    at push../node_modules/@antv/g2/esm/chart/view.js.View.render (view.js:146:1)

复现代码

const chart = new Chart({
  container,
  width: 800,
  height: 400,
});
const data = Array.from({ length: 10 }, (e, i) => {
  return {
    x: i,
    y: i,
  };
});
chart.data(data);
const line = chart.line();
line.position('x*y');
line.destroy();
chart.render();

不合法的用法!你需要做什么样的功能?

posted by hustcc over 2 years ago

这是一个最小可复现的 demo,所以显得不合理... 是实际使用中需要添加与删除图形,在删除之后再次添加图形然后调用 render 就会报错

posted by yinhangfeng over 2 years ago

哦哦,好的。

const line = chart.line();
line.position('x*y');
// 从容器中删除(当前没有 api,这么做有点 hack)
chart.geometries = chart.geometries.filter(e => e!== line)
line.destroy();
chart.render();
posted by hustcc over 2 years ago

Fund this Issue

$0.00
Funded

Pull requests