【发布时间】:2017-02-14 16:47:47
【问题描述】:
我正在尝试使用他们在 ES6 类中推荐的模式来遵循 React 的 no-bind 规则:
class Foo extends React.Component {
constructor() {
super();
this._onClick = this._onClick.bind(this);
}
render() {
return (
<div onClick={this._onClick}>
Hello!
</div>
);
}
_onClick() {
// Do whatever you like, referencing "this" as appropriate
}
}
但是,当我需要将参数传递给 _onClick 时,需要更改什么?
我尝试过类似的方法:
import {someFunc} from 'some/path';
class Foo extends React.Component {
constructor() {
super();
this._onClick = this._onClick.bind(this, a, b);
}
render() {
const {
prop1,
prop2
} = this.props;
return (
<div onClick={this._onClick(prop1, prop2}>
Hello!
</div>
);
}
_onClick = (a, b) => {
someFunc(a, b);
}
}
但是,这不起作用。需要修改什么?
【问题讨论】:
-
我不知道 React,但我猜如果参数已经绑定,那么处理程序应该保持不变:
<div onClick={this._onClick}>
标签: javascript reactjs ecmascript-6 bind