简体   繁体   中英

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. Is it possible to call an instance method from this method?

From MDN documentation

The static keyword defines a static method for a class. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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