question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using the-graph as a React component in a webpack environment

See original GitHub issue

(Dev without much front-end experience here, sorry for any imprecision)

I read through the existing issues and saw there is the intention of stripping this project of noflo-specific dependencies and Polymer. But I have one question: as it is now, can the-graph be used as a React component in a Webpack/CommonJS modules environment at all?

I tried creating a simple project just to see if I could use one of the React components, but just rendering this:

import React from "react";
import TheGraph from "the-graph/dist/the-graph";

console.log(TheGraph);

const TestComponent = () => (
  <h1>Hello world!</h1>
);

export default TestComponent;

Causes an Uncaught ReferenceError: React is not defined here. Does this happen because you are still in the process of migrating the code base to use CommonJS modules? Or am I doing something wrong? If it’s the former reason, is there any hack I can do to make this work?

Thanks for your work until now on this project, these graphs surely look great!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jonnorcommented, Dec 28, 2017

Here is an example App.js that works out-of-the-box with create-react-app:

import React, { Component } from 'react';

import './App.css';
import 'the-graph/themes/the-graph-dark.css';

import TheGraph from 'the-graph';
import fbpGraph from 'fbp-graph';

// Generate some graph contents programatically
function addNode(graph) {
  var id = Math.round(Math.random()*100000).toString(36);
  var component = Math.random() > 0.5 ? 'basic' : 'basic';
  var metadata = {
    label: component,
    x: Math.round(Math.random()*800),
    y: Math.round(Math.random()*600)
  };
  var newNode = graph.addNode(id, component, metadata);
  return newNode;
};
function addEdge(graph, outNodeID) {
  var nodes = graph.nodes;
  var len = nodes.length;
  if ( len<1 ) { return; }
  var node1 = outNodeID || nodes[Math.floor(Math.random()*len)].id;
  var node2 = nodes[Math.floor(Math.random()*len)].id;
  var port1 = 'out' + Math.floor(Math.random()*3);
  var port2 = 'in' + Math.floor(Math.random()*12);
  var meta = { route: Math.floor(Math.random()*10) };
  var newEdge = graph.addEdge(node1, port1, node2, port2, meta);
  return newEdge;
};
function populateGraph() {
    var graph = new fbpGraph.Graph();
    addNode(graph);
    addNode(graph);
    addNode(graph);
    addEdge(graph);
    return graph;
};

class App extends Component {
  render() {

    var componentLibrary = {
        basic: {
          name: 'basic',
          description: 'basic demo component',
          icon: 'eye',
          inports: [
            {'name': 'in0', 'type': 'all'},
            {'name': 'in1', 'type': 'all'},
            {'name': 'in2', 'type': 'all'}
          ],
          outports: [
            {'name': 'out', 'type': 'all'}
          ]
        },
    };

    var myGraph = populateGraph();

    return (
      <div className='the-graph-dark'>
        <TheGraph.App width={window.innerWidth} height={window.innerHeight} library={componentLibrary} graph={myGraph}/>
      </div>
    );
  }
}

export default App;

Closing this as fixed

2reactions
jonnorcommented, Dec 28, 2017

Screenshot of how above example should look: the-graph-react-app

The test app was actually using 16.2.0 (default in create-react-app). The very basics seems to work OK there, but React 15.6 will probably be the recommended/supported version for now. Compatibility fixes for React 16 that also works on 15.6 very welcomed!

Read more comments on GitHub >

github_iconTop Results From Across the Web

GitHub - customink/webpack-react-graph
A webpack plugin that generates a graph visualization of a React component tree ... the graph in the distributable of an application's webpack...
Read more >
Using Webpack with React, Pt. 1 - Toptal
In this Webpack tutorial, we demystify Webpack/React configuration. Go beyond Create React App with Webpack customization tailored to the needs of your app....
Read more >
Using environment variables in React | by Sam Pakvis - Medium
I'm going to show you two ways to set and use environment variables for your React projects with webpack: using npm scripts and...
Read more >
Getting Started - webpack
Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API.
Read more >
Create a To-do list web app from scratch using React Hooks ...
We will be building a todo list from start to end and we will be building it with the latest libraries such as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found