-2

What will be all good practical and frequent use cases for using static methods in ES6 classes?

Let me add one. React has concept of compound components.

class Toggle extends React.Component {

  static On = ({on, children}) => (on ? children : null)
  static Off = ({on, children}) => (on ? null : children)
  static Button = ({on, toggle, ...props}) => (
    <Switch on={on} onClick={toggle} {...props} />
  )
  state = {on: false}
  toggle = () =>
    this.setState(
      ({on}) => ({on: !on}),
      () => this.props.onToggle(this.state.on),
    )
  render() {
    return React.Children.map(this.props.children, child =>
      React.cloneElement(child, {
        on: this.state.on,
        toggle: this.toggle,
      }),
    )
  }
}

Usage

return (
    <Toggle onToggle={onToggle}>
      <Toggle.On>The button is on</Toggle.On>
      <Toggle.Off>The button is off</Toggle.Off>
      <Toggle.Button />
    </Toggle>
  )

I do intend to make this a reference to all of not so experienced devs in OOP design. So for that reason, there might not be a single best answer but you may get upvotes in the process of posting good answers from other members.

Note - Everyone might be aware of general utility functions/validations/etc. I'm sure there might be more powerful ones or rather unknown ones to hear of from senior community members here.

HalfWebDev
  • 7,022
  • 12
  • 65
  • 103
  • @jonrsharpe Does this question involves a list of books, features, tools, etc. Or will this question involve code use cases ? That's allowed in SO right? – HalfWebDev Nov 27 '18 at 08:43
  • You probably have no idea what you are asking about. What kind of answer do you expect? Static method are methods that can be called on the class def. directly. Do you expect them to give you money or make you coffee? – nicholaswmin Nov 27 '18 at 08:44
  • @NicholasKyriakides don't be in contradiction to yourself. Is that why you were the first to answer? – HalfWebDev Nov 27 '18 at 08:49
  • 2
    It involves a list of "etc.", yes; a list of use cases for static methods. The fact that they're also potentially code doesn't stop them being a list. *"there might not be a single best answer"* - there isn't, that's why it's not a valid question here. – jonrsharpe Nov 27 '18 at 09:42
  • What if I say the reverse? It's a list doesn't mean it will not have code and people won't learn. For the last line of best answer, if that's what you believe rather than contribution towards community knowledge and helping. Seems outright approach towards selfishness and points. I hope you as a senior folk is aware of the criticism of StackOverflow for same thing on internet. More you speak, more are you sounding one of those less welcoming folks geared towards rat race of points. – HalfWebDev Nov 27 '18 at 13:21

2 Answers2

1

What will be all good practical and frequent use cases for using static methods in ES6 classes?

A utility function that is irrelevant to an instance.

Take this example:

class Car {
   constructor() {
     this.steeringWheel = new SteeringWheel()
   }

   // Obviously can't be a static method, it deals
   // with THIS car's steering wheel.
   turn(direction) {
     this.steeringWheel.turn(direction)
   }

   // Can be static.
   static milesToKm(miles) {
     return miles / 0.6
   }
}
  • I might want to use milesToKm without instantiating anything, i.e just Car.milesToKm(100) works. I don't need to create a Car to call it.

  • The method itself never touches the instance.

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • This only makes sence if you want to use `milesToKm` outside of the `Car`. Otherwise I would declare `milesToKm` in the module scope, outside of the class. – Nikita Malyschkin Nov 27 '18 at 08:34
  • Of course you would, this is just for reference. Don't nitpick the example itself. – nicholaswmin Nov 27 '18 at 08:34
  • @NicholasKyriakides I appreciate, but we can try to stay relevant. Question says practical use cases. Everyone might be aware of general utility function/validations/etc. I'm sure there might be more powerful ones or rather unknown ones to hear of from community. – HalfWebDev Nov 27 '18 at 08:37
  • @kushalvm I find it irrelevant that you find this irrelevant. Why is this not a practical example? If you have a better one right up an answer instead of muddying this answer with nonsense. – nicholaswmin Nov 27 '18 at 08:38
  • @NicholasKyriakides and now perhaps you're realising why questions like this are unwelcome! – jonrsharpe Nov 27 '18 at 08:39
  • @jonrsharpe Yup, fully; Won't ever fall into this trap again. – nicholaswmin Nov 27 '18 at 08:40
-1

What will be all good practical and frequent use cases for using static methods in ES6 classes?

Shared singleton instances, like queues or websocket connections.