|
|
|
|
|
by cormacrelf
3360 days ago
|
|
Basically 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);
|
|