简体   繁体   English

如何从reactjs中的静态方法调用另一个方法?

[英]How to call another method from a static method in reactjs?

I just converted some obsolete events and now I am using 'getdirivedstatefromprops' which is static.我只是转换了一些过时的事件,现在我使用的是静态的“getdirivedstatefromprops”。 Is it possible to call an instance method from this method?是否可以从此方法调用实例方法?

From MDN documentation来自MDN 文档

The static keyword defines a static method for a class. static 关键字定义类的静态方法。 Static methods aren't called on instances of the class.不会在类的实例上调用静态方法。 Instead, they're called on the class itself.相反,他们在类本身上被调用。 These are often utility functions, such as functions to create or clone objects.这些通常是实用函数,例如创建或克隆对象的函数。

A static method doesn't require an instance of the class to exist, however you can't call an instance method until you instantiate the class.静态方法不需要类的实例存在,但是在实例化类之前不能调用实例方法。 For this reason you cannot call an instance method from a static method.因此,您不能从静态方法调用实例方法。

You should only use static methods to perform utility tasks that don't require an instance of the class to exist.您应该只使用静态方法来执行不需要类实例存在的实用程序任务。 That should inform your decision of when to use a static method.这应该告知您何时使用静态方法的决定。 If you need an instance of the class, create one and use it.如果您需要该类的一个实例,请创建一个并使用它。

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

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