-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shallowCompare module and use it in PureRenderMixin + tests #3355
Conversation
Add shallowCompare module and use it in PureRenderMixin + tests
@cpojer @sebmarkbage why dont we just create an class that inherits from Component? e.g: class PureCompoment extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return !React.addons.shallowCompare(this, nextProps, nextState)
}
}
class MyComponent extends PureCompoment {
// it gets shouldComponentUpdate for free
} |
@3den Because Javascript doesn't support multiple inheritance and using multiple "mixins" like this would be impossible. |
@dapetcu21 I think we don't need multiple inheritance nor mixins. I am suggesting that react should offer 2 kinds of components: |
@3den What happens when you want to be both a |
@jimfb true, I see your point. But I think it would be nice to have a something like a decorator just so that we don't have to copy and paste that function over and over... |
@3den Yes, that's called a "mixin". |
Also, sometimes copy-pasting a trivial three-line function can be a better solution than introducing abstraction and indirection. It makes it obvious where the method comes from, and easy to change its implementation if necessary later. |
This adds
shallowCompare
and adds tests for PureRenderMixin.This allows the following pattern:
Not super happy about this API.
Component.prototype.shouldComponentUpdate = React.addons.shallowCompare
seems like the better choice for the common case, but then using it would require callingReact.addons.shallowCompare.apply(this, arguments)
.