Consider the following higher order component (taken & "simplified" from the React docs):
function withSubscription(WrappedComponent, selectData) {
return class extends React.Component {
state = {
data: selectData(DataSource, props)
};
componentDidMount() {
DataSource.addChangeListener(this.handleChange);
}
componentWillUnmount() {
DataSource.removeChangeListener(this.handleChange);
}
handleChange = () => {
this.setState({ data: selectData(DataSource, this.props) });
}
render() {
return <WrappedComponent data={this.state.data} {...this.props} />;
}
};
}
In the render method, all the props initially owned by WrappedComponent are passed on by {...this.props}. However I still have trouble to understand why this happens, since (as far as I know) new function and class declarations have their own this value. This means that the this keyword should(?) belong to the newly returned class, but why is this not happening?