简体   繁体   English

ReactJS - 组件安装后需要导入/重新运行本地javascript文件

[英]ReactJS - After component mounts need to import/rerun local javascript file

Need to import "validation.js" file within another javascript file in reactjs once the main file render method is completes it execution 一旦主文件呈现方法完成执行,需要在reactjs中的另一个javascript文件中导入“validation.js”文件

import './Validations';

should render this JS file once the main component's render method complete its execution 一旦主要组件的render方法完成其执行,就应该呈现此JS文件

What you're trying to achieve is not really the way react likes to behave and probably is a mistake. 你想要实现的并不是反应喜欢的方式,而且可能是一个错误。 if you need some extra functionality to be available in your component you could define a class, put your external logic in there and then instantiate an object from that class in your component's constructor or render method, and use whatever functionality you need in from there. 如果您需要在组件中使用一些额外的功能,您可以定义一个类,将外部逻辑放在那里,然后在组件的构造函数或渲染方法中实例化该类中的对象,并使用您需要的任何功能。

You can use react-loadable library to lazy load javascript file. 您可以使用react-loadable库来延迟加载javascript文件。

import Loadable from 'react-loadable';

const LoadableTest = Loadable({
  loader: () => import('./validations.js'),
  loading() { // you could write your spinner while the file is being loaded.
    return <div>Loading...</div>
  }
});

class MyComponent extends React.Component {
  render() {
    return <LoadableTest/>;
  }
}

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

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