Skip to content
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

Let me use inheritance with componentDidMoubt #2012

Closed
AdamKyle opened this issue Aug 8, 2014 · 4 comments
Closed

Let me use inheritance with componentDidMoubt #2012

AdamKyle opened this issue Aug 8, 2014 · 4 comments

Comments

@AdamKyle
Copy link

AdamKyle commented Aug 8, 2014

Consider the following:

var MixinNameHere = {

    componentDidMount: function() {
         // Do some stuff yo!
     }
}

var ComponentNameHere = React.createClass({

     mixins: [MixinNameHere],

     componentDidMount: function() {
         // So some stuff here, then (or before) the parent should be called.
     }
});

The above will not work because as you can see I have declared the existence of componentDidMount() twice. This would be fair if we didn't have libraries such as underscore or similar libraries that make the use of _.extend which then lets us do:

ClassName.__super__.method.apply(this, arguments);

So my question is, as some one who likes to abstract common logic out and use inheritance (I mix backbone with react) - Why can I not use inheritance to say "did my component mount, yes do this - oh and call the parent

@syranide
Copy link
Contributor

syranide commented Aug 8, 2014

Duplicate component*-methods are all called, they are not replaced. Anyway, you are free to implement whatever form of class composition you want to create the object for React.createClass, mixins are just a convenience. I suspect React.createClass will also be deprecated as part of 0.12, or remain only as a helper (I'm not 100% sure about the details).

@AdamKyle
Copy link
Author

AdamKyle commented Aug 8, 2014

Why would they deprecate it? Its used across everything. Do they know what they are replacing it with? as for duplicate methods, yes they are called but it errors out stating that you have defined this else where. (I assumed it could be fixed with class composition)

@syranide
Copy link
Contributor

syranide commented Aug 8, 2014

@AdamKyle https://github.com/reactjs/react-future/blob/master/01%20-%20Core/05%20-%20Elements.js

Most of the information is kind of spread out, and in some ways it's simply being renamed I guess. But the idea is that you will supply the class and you can ask React to create an "element factory" from it (React.createClass currently creates the class and element factory). This means that you're free to use ES6 classes if you want, or any other class utility you prefer.

@zpao
Copy link
Member

zpao commented Aug 14, 2014

Why can I not use inheritance to say "did my component mount, yes do this - oh and call the parent

It's not clear to me what you really want. The code you have in your example calls both methods.

yes they are called but it errors out stating that you have defined this else where

I haven't seen this be the case. Only 1 lifecycle method restricts itself to be defined once (shouldComponentUpdate), the rest are all chained. If you had a repro case that would be helpful.

There's more discussion about the future of mixins in #1380.

There doesn't appear to be anything currently actionable here so I'm going to close out.

@zpao zpao closed this as completed Aug 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants