react-bind-decorator icon indicating copy to clipboard operation
react-bind-decorator copied to clipboard

Similarity with react-autobind?

Open markerikson opened this issue 9 years ago • 5 comments

Looks pretty similar to https://github.com/cassiozen/React-autobind . Any differences?

markerikson avatar Apr 02 '16 23:04 markerikson

Looks like that is 1) not a decorator, and 2) bound at the instance level instead of the prototype level. So my guess is that it is much slower (similar in performance to the ES6ConstructorBindVersion test case).

zackargyle avatar Apr 03 '16 00:04 zackargyle

Looks like I was right. It is actually the slowest implementation by far. These are the results for the basic use case (binding everything).

export class ReactAutobindVersion extends Component {
    constructor() {
        super();
        reactautobind(this);
    }
    fn1() {}
    fn2() {}
    fn3() {}
    render() {
        return <div onClick={this.fn1}>Hello world</div>;
    }
}
screen shot 2016-04-02 at 6 33 30 pm

zackargyle avatar Apr 03 '16 01:04 zackargyle

Two questions, then:

  1. How much practical difference is there going to be speed and performance-wise between these approaches in a real app?
  2. Is there a way to use react-bind-decorator as a plain function if a project isn't set up to use decorators?

markerikson avatar Apr 03 '16 01:04 markerikson

Skimming the code... would I use it as:

import bind from "react-bind-decorator";
class MyComponent extends Component{};

bind()(MyComponent);

markerikson avatar Apr 03 '16 01:04 markerikson

Yup! Might actually be nice to return target from the internal function so that you can do: export default bind()(MyComponent); If you want to contribute that would be an easy PR!

zackargyle avatar Apr 03 '16 01:04 zackargyle