react-diagrams icon indicating copy to clipboard operation
react-diagrams copied to clipboard

TypeError: Cannot read property 'getTotalLength' of null

Open isopterix opened this issue 6 years ago • 13 comments

Hi, i am unable to get the demo running.

I followed @cesarecaoduro post and have the same setup running. However, in my case I am not even getting to the blank screen. Instead react is throwing the error "TypeError: Cannot read property 'getTotalLength' of null".

Any ideas? Otherwise looks really cool. Would love to give it a shot at a process documentation project I am working on. Thanks.

Capture

isopterix avatar Apr 02 '20 09:04 isopterix

What is your React project setup? Is it server side rendered or something like that?

I've never had or seen this problem before.

renato-bohler avatar Apr 02 '20 11:04 renato-bohler

No, right now I am just using plain create-react-app with enabled typescript support.

isopterix avatar Apr 02 '20 12:04 isopterix

Looks like there was some kind of error somewhere in the copied code. I haven't figured out yet where exactly but after coping also the react component code shared by @cesarecaoduro rather than using the example one it its working now.

isopterix avatar Apr 02 '20 13:04 isopterix

i have created a clean project, a new file test.tsx, copy file demos\demo-simple (with deps ../helpers/DemoCanvasWidget) and it's not working, I'm getting the same result.

Cannot read property 'getTotalLength'

I made a npm link and until I can read the code the ref.current was null in the point that path was added to the Array

		this.props.link.setRenderedPaths(
			this.refPaths.map(ref => {
				return ref.current;
			})
		);

softov avatar Apr 19 '20 07:04 softov

in example demo-simple, it's fire when link1.addLabel('Hello World!');

zhanbst avatar May 11 '20 10:05 zhanbst

Hi, also running into this issue. It appears to only happen when using React.StrictMode during development. A production build does not show this issue.

Steps I took to replicate issue:

npx create-react-app my-app --template typescript
cd my-app
npm install  # Installs typescript types
npm install @projectstorm/react-diagrams closest lodash pathfinding paths-js emotion dagre mathjs resize-observer-polyfill @emotion/core @emotion/styled

Update src/App.tsx:

import React from "react";

import createEngine, {
  DefaultLinkModel,
  DefaultNodeModel,
  DiagramModel,
} from "@projectstorm/react-diagrams";
import { CanvasWidget } from "@projectstorm/react-canvas-core";
import "./App.css";

function App() {
  //1) setup the diagram engine
  var engine = createEngine();

  //2) setup the diagram model
  var model = new DiagramModel();

  //3-A) create a default node
  var node1 = new DefaultNodeModel({
    name: "Node 1",
    color: "rgb(0,192,255)",
  });
  node1.setPosition(100, 100);
  let port1 = node1.addOutPort("Out");

  //3-B) create another default node
  var node2 = new DefaultNodeModel("Node 2", "rgb(192,255,0)");
  let port2 = node2.addInPort("In");
  node2.setPosition(400, 100);

  // link the ports
  let link1 = port1.link<DefaultLinkModel>(port2);
  link1.getOptions().testName = "Test";
  link1.addLabel("Hello World!");

  //4) add the models to the root graph
  model.addAll(node1, node2, link1);

  //5) load model into engine
  engine.setModel(model);
  return <CanvasWidget engine={engine} className="canvas" />;
}

export default App;

Temporary "fix" by:

  • removing <React.StrictMode> tags from src/index.tsx
  • changing src/index.css to include:
    .canvas {
      height: 100vh;
    }
    

Trial and error approach (not ideal). Production builds work with <React.StrictMode> tags.

crum-uk avatar May 29 '20 11:05 crum-uk

@Crumzor thank you for the write up, I was experiencing all the exact same issues as you. You're solution works but this should really be unnecessary for a project claiming

"a super simple, no-nonsense diagramming library written in react that just works"

It does not, it took a day of tracking down errors and parsing repo issues.

DannyHinshaw avatar Jun 03 '20 15:06 DannyHinshaw

Steps I took to replicate issue:

These should really be in the docs, would have saved me hours of trying as I'm not really at home with npm and/or TypeScript

oliverrahner avatar Jun 03 '20 18:06 oliverrahner

This also caught me - definitely should be added to docs

johnsmithlon avatar Jun 05 '20 17:06 johnsmithlon

@Crumzor 's walkthrough is perfect. Thank you!

The Official Guide misses two node modules that are needed to be installed manually:

  • resize-observer-polyfill
  • @emotion/styled

TomHermanSAP avatar Jul 05 '20 13:07 TomHermanSAP

For googlers, anyone encountering this error on Firefox will instead receive the message, "TypeError: path is null".

patrickjm avatar Aug 14 '20 01:08 patrickjm

The fix proposed by @Crumzor was 99% the fix for me - the CSS I needed was a little different:

#root {
  height: 100vh;
}

#root > div {
  height: 100vh;
}

DavidDionise avatar Sep 05 '21 01:09 DavidDionise

can someone tell me the steps for fixing the issue, it is very annoying? I am trying to add something like a icon on label. Thank you in advance!!!!

abhishek-doe avatar Feb 07 '24 07:02 abhishek-doe