Comments (6)
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.
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.
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.
@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.
works. thanks a lot
from react-stamp.
You're welcome!
from react-stamp.
Related Issues (20)
- How to enable webpack hot module reload with that approach? HOT 1
- remove carret on stamp-utils version HOT 4
- Autobind of methods? HOT 2
- Automatically setting displayName for a component HOT 1
- Real code example in README
- An in-range update of eslint is breaking the build 🚨 HOT 5
- An in-range update of babel-core is breaking the build 🚨 HOT 2
- An in-range update of babel-loader is breaking the build 🚨 HOT 4
- An in-range update of rimraf is breaking the build 🚨 HOT 1
- An in-range update of babel-preset-es2015 is breaking the build 🚨 HOT 1
- An in-range update of babel-eslint is breaking the build 🚨 HOT 3
- An in-range update of babel-preset-stage-2 is breaking the build 🚨
- An in-range update of babel-cli is breaking the build 🚨
- An in-range update of react is breaking the build 🚨
- An in-range update of react-addons-test-utils is breaking the build 🚨
- An in-range update of react is breaking the build 🚨
- An in-range update of react is breaking the build 🚨
- An in-range update of react is breaking the build 🚨 HOT 1
- An in-range update of react-addons-test-utils is breaking the build 🚨
- Anyone using react-stamp? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-stamp.