Rendering vis.js network into container via React.js

optional picture optional · Nov 24, 2016 · Viewed 8k times · Source

how do I render a vis.js network into a concrete html container?
I've tried the following, however it does not work:

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

Or should I render it the following way?

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

While the elements "app" and "network" are in a wrapper html container.

I would appreciate a solution that the vis.js network is rendered into the root document:

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

Cheers!

Answer

Gino Llerena picture Gino Llerena · Nov 24, 2016

this works for me:

 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/