question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

decorator around mixins

See original GitHub issue

I saw the deprecation warnings for using mixins are reverted with 0.13.3, so the official story is now to use mixins, always. I think it’d be nice to also support ES6 classes and not force everyone to use mixins in their code if they’d prefer not to.

I came up with a first draft on how it could be implemented by reusing the mixin internally and exposing a decorator function.

https://jsfiddle.net/ksmth/t34ak1q3/10/

// ES7
@routerState
class MyComopnent extends React.Component {
    // ...
}
// ES6
class MyComopnent extends React.Component {
    // ...
}
MyComopnent = routerState(MyComopnent);
// React.createClass without mixins
var MyComopnent = React.createClass({
    // ...
});
MyComopnent = routerState(MyComopnent);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
juhaeleecommented, Jun 23, 2015

I think @taurose makes a good point about the breaking refs and animations. HoC might solve one problem, but it could cause more problems as well. Is the best way to access the router methods for an ES6 class is through the contextType class property?

Component.contextTypes = {
  router: PropTypes.func.isRequired
};

Then to access router methods, this.context.router.[method]

1reaction
agundermanncommented, Jun 19, 2015

Thanks for sharing 👍

I wonder if it wouldn’t be preferable to use class inheritance though, in order to avoid the extra wrapper component which breaks refs and animations.

Something like

var stateDecorator = (Component) => {
  class Derived extends Component {}

  Object.assign(Derived.prototype, StateMixin); 
  return Derived;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between a mixin and the decorator ...
A mixin is apt when you're adding some behavior to your class. ... A decorator on the other hand is more of a...
Read more >
Decorators and Mixins in Lightning Web Components
In essence, a decorator is a function that can modify a class, or any of its properties and methods.
Read more >
Enhancing Mixins with Decorator Functions - Justin Fagnani
Adding instanceof support and mixin caching to subclass factories with mixin decorator functions.
Read more >
Decorators and Mixins in Lightning Web Components - DZone
In essence, a decorator is a function that can modify a class, or any of its properties and methods.
Read more >
Decorators and Mixins – Cross-cutting Aspects
Chapter 8. Decorators and Mixins – Cross-cutting Aspects. A software design often has aspects that apply across several classes, functions, or methods.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found