首页 > 解决方案 > React / D3 错误“未捕获的 TypeError:无法读取属性 'ownerDocument' of null”

问题描述

我正在尝试使用d3但在反应的上下文中将一些数据绑定到 dom 元素。我找不到 d3 代码的任何问题,所以我想知道我是否错误地处理了 React 生命周期/引用。

import React from "react";
import { select, selectAll } from "d3-selection";

class Test extends React.Component {
  componentDidMount() {
    this.makeD3();
  }

  makeD3 = () => {
    console.log("maked3");
    selectAll(this.refs.node).data([1, 2, 3, 4]);
    select(this.refs.node).data([1, 2, 3, 4]);  // try just select?
  };

  render() {
    return (
      <div ref="node">test</div>
    );
  }
}

标签: reactjsd3.js

解决方案


推荐阅读