简体   繁体   English

React 抛出一个错误,说 setTimeout 不是一个函数?

[英]React throwing an error saying that setTimeout is not a function?

Im trying to use the XTK library in react and keeps telling me that the setTimeout function is not a function.我试图在反应中使用 XTK 库,并一直告诉我 setTimeout 函数不是函数。 My code seems to be pretty clean and it functions well if I just use it directly with the html.我的代码似乎很干净,如果我直接将它与 html 一起使用,它运行良好。 However Im trying to get it to work in a react component and it just fails straight up.然而,我试图让它在反应组件中工作,但它直接失败了。 Can anyone tell me what Im doing wrong or where I can look to fix this?谁能告诉我我做错了什么或者我可以在哪里解决这个问题?

error message错误信息

code for the component:组件代码:

import React from 'react';


import window from 'xtk';
// window.X is now available

export default class VtkGeometryRenderer extends React.Component {
  constructor (){
    super();
  }
  shouldComponentUpdate(){
    return false;
  }
  componentDidMount(){
    // create a new 3d renderer
    var r = new  window.X.renderer3D();
    //r.container = 'r';
    r.init();

    // create a mesh from a .vtk file
    var skull = new window.X.mesh();
    skull.file = 'http://x.babymri.org/?avf.vtk';

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

    // .. and render it
    r.render();

  }
  render() {

    return (
          <div
              ref={(c) => {
              this.rootContainer = c;
            }}
          />
        );
};

Oh the npm package just adds X on the window namespace.哦,npm 包只是在窗口命名空间上添加了X

Change:改变:

import window from 'xtk'

to:到:

import 'xtk'

This will add X to the existing window object, instead of reassigning it like you are doing with that import statement.这会将X添加到现有的window对象,而不是像使用 import 语句那样重新分配它。

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

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