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.