简体   繁体   English

我该如何调用组件内部的方法,该方法是从react.js中的第3方库更新的?

[英]How do I call a method inside component did update from a 3rd party library in react.js?

Ok so Im trying to call a method from another library called the xtk library I have tested the JavaScript code on its own and it works just fine. 好的,我试图从另一个名为xtk库的库中调用一个方法,我已经对JavaScript代码进行了测试,并且效果很好。 Now Im just trying to create a component react that will run it. 现在我只是试图创建一个组件反应将运行它。 Im a newbie when it comes to both js and react so I might not even be asking the right question here. 我是一个新手,涉及到js和响应,所以我什至在这里可能都不问正确的问题。

componentDidMount = function(){
  this.v = X.renderer3D(this.refs.v);
  this.v.container ='v';
  this.v.init();
  this.randF = X.mesh.randF(this.refs.randf);
  this.file.randF = '../Assets/rgrid.vtk';
  this.v.add(this.randF);
  this.v.render();
}

Where Im essentially trying to execute this javascript code with that react.js code. 我本质上是试图用那个react.js代码执行这个javascript代码。

window.onload = function() {

  // create and initialize a 3D renderer
  var r = new X.renderer3D();
  r.init();

  // create a new X.mesh
  var skull = new X.mesh();
  // .. and associate the .vtk file to it
  skull.file = 'http://x.babymri.org/?skull.vtk';
  // .. make it transparent
  skull.opacity = 0.7;

  // .. add the mesh
  r.add(skull);

  // re-position the camera to face the skull
  r.camera.position = [0, 400, 0];



  r.render();

};

I keep constantly getting errors thrown at me saying that render3D is not a class, that X is not a constructor and so on. 我一直不断地向我抛出错误,说render3D不是类,X不是构造函数,依此类推。 I have essentially spent the last few days trying to figure this out mostly on my own so any advice at all, even of where to look would be absolutely great. 从本质上讲,我已经花了几天的时间来尝试主要靠我自己解决这个问题,因此,即使有任何建议,无论从哪方面来看,它都绝对是很棒的。

thank you. 谢谢。

Ok this took me a long time to figure out so I want other people to not go through this. 好的,这花了我很长时间才弄清楚,所以我希望其他人不要经历这个。

You cannot simply install xtk, you have to eject from create-react-app, Delete your config.lock file and all of your modules, then run npm install on it and run the xtk install on it. 您不能简单地安装xtk,必须从create-react-app弹出,删除config.lock文件和所有模块,然后在其上运行npm install并在其上运行xtk安装。

Then you will be able to use it. 这样您就可以使用它了。

This is just in general the way to use full robust 3rd party libraries without having to build everything from scratch. 通常,这只是使用完整的强大的第三方库而无需从头开始构建所有内容的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM