I want to create a util class for my react app. util classes should be static classes which cannot be instantiated as I know. So, what is the proper way to create a util class in javascript and how we can make it static?
You can define a class that contains only static
methods, it may look something like as follows:
class Util {
static helper1 () {/* */}
static helper2 () {/* */}
}
export default Util;
However, since you don't need a possibility to instantiate an object of the utility class, chances are that you don't really need a class and a simple module that exports utility functions will suite your demand better:
const helper1 = () => {/* */};
const helper2 = () => {/* */};
export default {
helper1,
helper2
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With