在Reactjs中,从函数内部调用组件函数
Calling a component function from within a function in Reactjs
我有一个组件,它的布局是这样的:
var Entry = React.createClass({
// executes code on a button press.
onButtonClick: function (event) {
// (do stuff)
},
// generates the entry list with a button and some info.
render: function() {
var entryList= this.props.data.map(function(entry) {
// (convert timestamp into relative time, add some tags etc)
return (
<p> entry.information </p>
<a onClick={onButtonClick} className="btn right" id={entry.link}>
go
</a>
);
});
// returns the html of the component
return (
<div className="entryList">
{entryList}
</div>
);
}
});
我希望能够从渲染函数中的entryList变量内运行函数onButtonClick,但我似乎无法弄清楚如何做到这一点。当运行它时,控制台显示未定义onButtonClick。
Uncaught ReferenceError: onButtonClick is not defined
如何"转义"函数?我认为this.props.data.map(function(items) {});
使问题复杂化因为我可以很好地访问它如果我像这样移动按钮
// returns the html of the component
return (
<div className="entryList">
<a onClick={this.onButtonClick} className="btn right">go</a>
{entryList}
</div>
);
}
});
谢谢你的帮助!
您的代码没有按照您期望的方式工作的原因是因为上下文this
在传递给map
的匿名函数中发生了变化。map
接受第二个可选参数,它表示回调将使用的this
的值。因此,只需将this
作为map
的第二个参数就可以解决您的问题。
var entryList = this.props.data.map(function(entry) {
...
}, this);
使用ES2015的开发人员也可以使用箭头函数来自动绑定正确的this
上下文。
var entryList = this.props.data.map(entry => {
...
});
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
上下文变了。所以你可以这样做。
var Entry = React.createClass({
// executes code on a button press.
onButtonClick: function (event) {
// (do stuff)
},
// generates the entry list with a button and some info.
render: function() {
var self = this;
var entryList= this.props.data.map(function(entry) {
// (convert timestamp into relative time, add some tags etc)
return (
<p> entry.information </p>
<a onClick={self.onButtonClick} className="btn right" id={entry.link}>
go
</a>
);
});
// returns the html of the component
return (
<div className="entryList">
{entryList}
</div>
);
}
});
或简称
var Entry = React.createClass({
// executes code on a button press.
onButtonClick: function (event) {
// (do stuff)
},
// generates the entry list with a button and some info.
render: function() {
var entryList= this.props.data.map(function(entry) {
// (convert timestamp into relative time, add some tags etc)
return (
<p> entry.information </p>
<a onClick={this.onButtonClick} className="btn right" id={entry.link}>
go
</a>
);
},this);
// returns the html of the component
return (
<div className="entryList">
{entryList}
</div>
);
}
});
相关文章:
- 调用函数内部的函数
- javascript函数内部的代码用逗号而不是分号分隔
- javascript无法重新定义函数内部的全局对象
- 如何从函数内部的这个变量中获取值
- appendChild在函数外部工作,但在函数内部不工作
- 在jquery函数内部设置来自jquery函数的var;t运行
- 一个'var'在函数内部声明
- 为什么(如何)'这'从函数内部调用回调时发生更改
- 函数内部未定义的输入值
- 在Meteor中如何将数据从函数内部复制到其他模板
- 如果在构造函数内部为else,则Javascript是可选的
- 我对“;返回true"嵌套函数内部;t工作
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- javascript,将参数传递给函数内部的闭包中的回调
- 匿名自执行js函数内部的全局变量在外部仍然可用
- 使用PHP和Javascript在函数内部传递alphaneumeric值作为参数时出错
- 访问函数内部的Polymer方法
- 更改函数内部的全局变量而不调用它
- 如何在函数内部为jquery工具提示调用不同的var字符串
- 构造函数内部的事件处理