-
-
Notifications
You must be signed in to change notification settings - Fork 348
Idea: Introduce Observer
as higher order component
#138
Comments
A little enhancement maybe? export const Observer = observer(props => React.createElement(props.children, props, null)); Should work the same way, but one can provide any react component (aside from function) and pass props: <Observer propForMyCmp="hello">{MyCmp}</Observer> |
@urugator personally I think that is a bit confusing to read? |
@mweststrate Its the same as in your example, you can still write the render callback inline, it just exploits the fact that render callback is actually valid react (stateless) component (also the restrictions are the same - for example the render callback must return single react element). So you can replace render callback by any smarter (class based, unobservable) react component anytime. |
@urugator you might be right, gonna give it a try :) |
I like it, so long as it doesn't become the default. More options for integration are better for a library, and lots of people (though not I), prefer using HoC style in React. Personally, though, I simply have a top level component that subclasses React.Component, and applies @observer onto it, then all my application components extend from that. |
@urugator played with your suggestion, but it was more confusing to read, and also kill static typing on props etc. So I'll keep it as is. Thanks for the suggestion though :) |
Implemented in |
Shipped with 4.0.0 :) |
Just wanted to say this pattern works great for me, most important is the fact that it doesn't alter the lifecycle methods which I use for server rendering. |
Alternative syntax for using
observer
as decorator / higher order component:Pros:
Component
Cons:
this
/this.props
/ might be dereferencing accidentally in host component instead of the observer regionN.b. implementation would simply be (not tested, might be missing something):
The text was updated successfully, but these errors were encountered: