5 ms·
Yeah I still use `React.createClass({})` because... autobinding. Also wish someone would explain the draw of ES6 classes. React is about composition, not inher
by linkmotif 10y ago
Yeah I still use `React.createClass({})` because... autobinding.
Also wish someone would explain the draw of ES6 classes. React is about composition, not inheritance. Have never seen a `React.Component` extended.
- fiatjaf 10y agoI've seen one. It is the horror, the horror.
- acemarke 10y agoSee my comment downthread regarding intended use of React.Component: https://news.ycombinator.com/item?id=14065106 https://news.ycombinator.com/item?id=14065106 .
- linkmotif 10y agoThanks. I responded there.
- cormacrelf 10y agoBasically nobody does this, as far as I know. I think some of the big component authors like Wijmo do, but not most people, it's way too messy. The composition is usually in the form of higher order components. It's very simple to wrap a function with another one, with more or different functionality. Classes just make it equally as simple when the wrapped component has its own state to manage. Point is, nobody really cares whether they're ES6 or createClass, because nobody's actually doing inheritance. It's just that we JS devs like to stay on the bleeding edge, and we like to think that we're converging on standards even if that's a silly dream. Example: show a spinner for 1 second before displaying. const F = (props) => <div>{props.content}</div>; class C extends React.Component { state = { initial: "state" }; render = () => <div>{this.state.initial}</div>; } function delayWithSpinner(WrappedComponent) { return class extends React.Component { state = { showSpinner: true }; stop = () { this.setState({ showSpinner: false }); } render() { return this.state.showSpinner ? <Spinner duration={this.props.duration} onCompletion={this.stop}/> : <WrappedComponent {...this.props}/> } } } // exactly the same composition pattern for // both classes and functions. const SlowC = delayWithSpinner(C); const SlowF = delayWithSpinner(F);
- boubiyeah 10y agoES6 classes are a complete mess brought by corporate people loving OO, even in a setting that doesn't make sense. You can "autobind" with this notation: myMethod = () => { } but it doesn't look as good as myMethod() {} and you have to remember this EVERY TIME you add a method. Also, you never need to use the constructor in React. Just do: state = {} in the body of the class. You may need babel presets #918718$$&ééàdi for it to work though.
- zdragnar 10y agoClasses and inheritance are features of type systems, not OOP. Plenty of functional programming JS libraries use classes for testing types (for example, the Either monads' Left and Right).
- paulddraper 10y agoClasses and class inheritance are features of class-based systems, yes. Prototypes and prototype inheritance are features of prototype based systems.
- icc97 10y agoReact themselves say they never use it [0] > At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies. [0]: https://facebook.github.io/react/docs/composition-vs-inheritance.html#so-what-about-inheritance https://facebook.github.io/react/docs/composition-vs-inherit...
- crgwbr 10y agoWe have a fairly large react codebase with some components using inheritance. It's not the solution to every problem certainly, but sometimes an abstract base component can be useful.