我有一个组件使用componentDidMount期间需要的文件。
我有两个页面使用此组件。它在第一页上工作正常,但是一旦我切换到另一页,就根本不会调用它。而且,它并不取决于所需的页面:它总是在第一次运行,但在第二次运行。
这是代码:
componentDidMount() {
try {
require('./data');
} catch (e) {
// Do nothing.
// No error, even on the second time
}
}
正在调用componentDidMount(确定了一些console.log),并且找到了文件(也在console.log上进行了检查)。
这是data.js文件:
console.log(window); // display something the first time, but nothing happens on the second time
if (typeof window !== 'undefined') {
const d3 = require('d3');
const Datamap = require('datamaps');
d3.selection.prototype.moveToFront = function() {
return this.each(function() {
this.parentNode.appendChild(this);
});
};
(function() { /* some code */ } )
}
对此有任何想法吗? 我的猜测之一就是没有正确卸载并重新安装它,但不知道如何检查或修复它