Git Product home page Git Product logo

Comments (6)

amelon avatar amelon commented on June 10, 2024

Actually if you don't use React.createClass there is no automatic binding (https://toddmotto.com/react-create-class-versus-component/).

There's at least two ways to avoid bind.

first one - double colons

const container = {
  state:{
    val: 'click me'
  },
    clickMe(evt) {
        this.setState({val: 'clicked'});
    },
    render() {
        return (
            <div className="clearfix">
                <a onClick={::this.clicMe}>{this.state.val}</a>
            </div>
        );
    }
}

This one is equivalent to <a onClick={this.clicMe.bind(this}>{this.state.val}</a> (http://stackoverflow.com/questions/31220078/javascript-double-colon-es7-proposal)

second one - fat arrow

const container = {
  state:{
    val: 'click me'
  },
    clickMe(evt) {
        this.setState({val: 'clicked'});
    },
    render() {
        return (
            <div className="clearfix">
                <a onClick={(e) => this.clicMe(e)}>{this.state.val}</a>
            </div>
        );
    }
}

This one is equivalent to

...
    render() {
        var _this = this
        return (
            <div className="clearfix">
                <a onClick={function(e) {_this.clicMe(e)}}>{this.state.val}</a>
            </div>
        );
    }
...

(https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/)

For both cases you must use babel but as you use import it should be the case.
For double colon you have to use babel stage 0 (http://babeljs.io/docs/plugins/preset-stage-0/).

from react-stamp.

troutowicz avatar troutowicz commented on June 10, 2024

This is by design. We did not want automatic binding as experienced with React.createClass. @amelon has recommended two good alternatives to .bind().

from react-stamp.

a-ursino avatar a-ursino commented on June 10, 2024

Hi
yes, i know the double colons feature of ES7 and fat arrow but i want only to avoid this problem jsx-no-bind

thanks anyway

from react-stamp.

troutowicz avatar troutowicz commented on June 10, 2024

@killanaca, ah gotcha. Try using the init method.

const component = {
  init () {
    this.state = {
      val: 'click me'
    };

    this.onClick = this.onClick.bind(this);
  },

  onClick (e) {
    this.setState({ val: 'clicked' });
  },

  render () {
    return (
       <div>
         <a onClick={this.onClick}>{this.state.val}</a>
       </div>
    );
  }
};

from react-stamp.

a-ursino avatar a-ursino commented on June 10, 2024

works. thanks a lot

from react-stamp.

troutowicz avatar troutowicz commented on June 10, 2024

You're welcome!

from react-stamp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.