altjs / utils

A collection of utils for alt.js
37 stars 25 forks source link

Add afterSetState hook to connectToStores #4

Closed ianks closed 8 years ago

ianks commented 8 years ago

Sometimes it is nice to be able to have a method be invoked whenever the state changes on the object. An example of this might read:

 class MyComponent extends React.Component {
   static getStores(props) {
     return [myStore]
   }
   static getPropsFromStores(props) {
     return myStore.getState()
   }
   static storeDidChange(props) {
     messenger.notify(props);
   }
   render() {
     // Use this.props like normal ...
   }
 }

 MyComponent = connectToStores(MyComponent)
ianks commented 8 years ago

I think the name could be better. What about storeDidChange to be more conventional with React lifecycle hooks?

Also, I was thinking this might be useful to define as an instance method as opposed to a static method. Thoughts @goatslacker?

EDIT: Renamed to storeDidChange