Let's say App
component is wrapped by bus, which is exported as withBus
. Here's pseudo-ish code.
class App extends Component {
constructor(props) {
super(props);
this.state = {};
}
handleSomeEvent(e) {
this.setState({ [e] });
}
render() {
return ( ... );
}
}
export default withBus(App);
Here, handleSomeEvent
is defined in the App
component. It invokes setState
.
Is it possible for the wrapping component (withBus
) to hijack / override, App's setState
instance so that every call to this.setState
in App is proxied to a method in withBus
which does some stuff, then invokes setState back on App
?
Important Point
- App shouldn't have to implement anything for this functionality.
In this example, the objective is to add functionality to setState without mudding up react's API.