通过React.js将vis.js网络渲染到容器中

时间:2016-11-24 18:20:17

标签: javascript reactjs vis.js

如何将vis.js网络渲染到具体的html容器中? 我尝试了以下内容,但它不起作用:

<div id="network">
   {new vis.Network("network", data, options)}
</div>

或者我应该按照以下方式渲染它?

ReactDOM.render(<App />, document.getElementById('app'));
ReactDOM.render(<Network />, document.getElementById('network'));

虽然元素“app”和“network”位于包装器html容器中。

我希望将vis.js网络呈现到根文档中的解决方案:

 ReactDOM.render(<App />, document.getElementById('root'));

干杯!

5 个答案:

答案 0 :(得分:9)

这对我有用:

 var nodes = new vis.DataSet([
        {id: 1, label: 'Node 1'},
        {id: 2, label: 'Node 2'},
        {id: 3, label: 'Node 3'},
        {id: 4, label: 'Node 4'},
        {id: 5, label: 'Node 5'}
    ]);

    // create an array with edges
    var edges = new vis.DataSet([
        {from: 1, to: 3},
        {from: 1, to: 2},
        {from: 2, to: 4},
        {from: 2, to: 5}
    ]);

var data = {
        nodes: nodes,
        edges: edges
    };
    var options = {};

    // initialize your network!


var VisNetwork = React.createClass({

    componentDidMount(){
            var network = new vis.Network(this.refs.myRef, data, options);
  },

  render: function() {
    return <div ref="myRef"></div>;
  }
});

ReactDOM.render(
  <VisNetwork />,
  document.getElementById('container')
);

https://jsfiddle.net/ginollerena/69z2wepo/63263/

答案 1 :(得分:4)

这是一个使用功能组件和React钩子的现代示例。

package.json

{
  "dependencies": {
    "react": "16.13.0",
    "react-dom": "16.13.0",
    "vis-network": "7.4.0"
  }
}

VisNetwork.js

import React, { useEffect, useRef } from 'react';
import { DataSet, Network} from 'vis-network/standalone/esm/vis-network';

const VisNetwork = () => {
  // A reference to the div rendered by this component
  const domNode = useRef(null);

  // A reference to the vis network instance
  const network = useRef(null);

  // An array of nodes
  const nodes = new DataSet([
    { id: 1, label: 'Node 1' },
    { id: 2, label: 'Node 2' },
    { id: 3, label: 'Node 3' },
    { id: 4, label: 'Node 4' },
    { id: 5, label: 'Node 5' }
  ]);

  // An array of edges
  const edges = new DataSet([
    { from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 2, to: 5 }
  ]);

  const data = {
    nodes,
    edges
  };

  const options = {};

  useEffect(
    () => {
      network.current = new Network(domNode.current, data, options);
    },
    [domNode, network, data, options]
  );

  return (
    <div ref = { domNode } />
  );
};

export default VisNetwork;

答案 2 :(得分:3)

从React 16.3开始,建议使用React.createRef Docs

以16.3样式更新@Gino的答案。

import { DataSet, Network } from 'vis';
import React, { Component, createRef } from "react";

const nodes = new DataSet([
  { id: 1, label: 'Node 1' },
  { id: 2, label: 'Node 2' },
  { id: 3, label: 'Node 3' },
  { id: 4, label: 'Node 4' },
  { id: 5, label: 'Node 5' }
]);

// create an array with edges
const edges = new DataSet([
  { from: 1, to: 3 },
  { from: 1, to: 2 },
  { from: 2, to: 4 },
  { from: 2, to: 5 }
]);

const data = {
  nodes: nodes,
  edges: edges
};
const options = {};

// initialize your network!


class VisNetwork extends Component {

  constructor() {
    super();
    this.network = {};
    this.appRef = createRef();
  }

  componentDidMount() {
    this.network = new Network(this.appRef.current, data, options);
  }

  render() {
    return (
      <div ref={this.appRef} />
    );
  }
}

答案 3 :(得分:3)

这是现代的打字稿方式:

import React, { useEffect, useRef } from "react";
import {
  DataSet,
  Network,
  Options,
  Data,
} from "vis-network/standalone/esm/vis-network";

export const LogicTreePanel = () => {
  // A reference to the div rendered by this component
  const domNode = useRef<HTMLDivElement>(null);

  // A reference to the vis network instance
  const network = useRef<Network | null>(null);

  // An array of nodes
  const nodes = new DataSet([
    { id: 1, label: "Node 1" },
    { id: 2, label: "Node 2" },
    { id: 3, label: "Node 3" },
    { id: 4, label: "Node 4" },
    { id: 5, label: "Node 5" },
  ]);

  // An array of edges
  const edges = new DataSet([
    { from: 1, to: 3 },
    { from: 1, to: 2 },
    { from: 2, to: 4 },
    { from: 2, to: 5 },
  ]);

  const data: Data = {
    nodes,
    edges,
  };

  const options: Options = {};

  useEffect(() => {
    if (domNode.current) {
      network.current = new Network(domNode.current, data, options);
    }
  }, [domNode, network, data, options]);

  return (
    <div
      ref={domNode}
    />
  );
};

答案 4 :(得分:0)

我正在尝试做类似的事情,但是我正在使用第三方库(Apollo),该库使用React Hooks,并且在类组件中使用时出现错误,因此我切换到了功能组件。 (我认为这与没有挂钩的类组件有关,但是Apollo挂钩用于从组件本身调用的函数中,因此我不太确定它们如何组合在一起。)

将ref与功能组件一起使用有点不同。我认为有一种使用useEffect钩子的方法,但是在我的特定用例中,我在渲染之前就钩子触发了一个问题(因为钩子被设置为在数据更改时触发),是动态的),并且参考仍然不可用。

要解决此问题,一种解决方案似乎是使用useCallback钩子,当将引用附加到节点或从节点分离引用时,该钩子将被触发。我基本上是从这里开始的,它描述了具有类似前提条件的情况:https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node

我刚刚实现了它,所以我没有时间确切地了解正在发生的事情以及以后将要发生的事情,但是下面设置的方式应该使它在data发生更改时触发(因为它在传递给[data]的依赖项列表useCallback中)。显然,在此示例中不会发生这种情况,但我认为我会保留它。

请注意,这已从我的特定示例中进行了修改,并且我实际上没有运行下面的代码,因此可能会有一些错误,但是我认为它应该指出其他正确的方向。

我对React真的很陌生,所以我不太确定这是解决此问题的最合适/最佳/最惯用的方法,因此,如果有人有其他意见,我很想听听。

import { DataSet, Network } from 'vis';
import React, { Component, createRef } from "react";

const nodes = new DataSet([
  { id: 1, label: 'Node 1' },
  { id: 2, label: 'Node 2' },
  { id: 3, label: 'Node 3' },
  { id: 4, label: 'Node 4' },
  { id: 5, label: 'Node 5' }
]);

// create an array with edges
const edges = new DataSet([
  { from: 1, to: 3 },
  { from: 1, to: 2 },
  { from: 2, to: 4 },
  { from: 2, to: 5 }
]);

const data = {
  nodes: nodes,
  edges: edges
};
const options = {};

// initialize your network!


const VisNetwork = () => {

  const triggeredRef = React.useCallback( node => {
    if ( node === null )
      return;

    const network = new Network(node, data, options);
  }, [data]);

  render() {
    return (
      <div ref={triggeredRef} />
    );
  }
}