如何将普通的javascript助手类导入React? [英] How to import plain javascript helper classes into React?
本文介绍了如何将普通的javascript助手类导入React?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用ES6风格编写的React类,例如:
I have a React class that I have written in ES6 style such as:
export default class App extends React.Component {
constructor(props) {
super(props);
let HClass = new HelperClass();
}
}
同一个文件中存在的我的帮助器类如下:
And my helper class, which exists within the same file, looks like:
class HelperClass {
constructor() {
this.somevar="";
}
some_function= () => {
//do work
}
}
但是,当尝试构造和运行'some_function'方法时,我收到TypeErrors
指出该函数未定义.
However, when trying to construct and run the 'some_function' method, I receive TypeErrors
stating that the function is not defined.
我的问题是:
- 实例化HelperClass时我做错了什么?
- 我应该做些类似反应"的事情,而不是这些助手类吗?它们什么也没渲染,但是我需要它们来管理我从ActiveMQ处理的消息.
谢谢!
推荐答案
当前,new HelperClass()
仅在构造函数内部可用.您只能在此处使用some_function
.
Currently, new HelperClass()
is only available inside your constructor. You can use the some_function
there only.
通常,您会这样做:
要在特定方法内使用:
// outside the constructor
myMethod() {
let HClass = new HelperClass();
HClass.some_function();
}
用于任何方法:
// inside the constructor
this.props.HClass = new HelperClass();
// to call
this.props.HClass.some_function();
或者,简单地使用这个:
Or, simply using this:
// inside the constructor
this.HClass = newHelperClass();
// to call
this.HClass.some_function();
这篇关于如何将普通的javascript助手类导入React?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文