React.js: 从子级到父级的 onClick 函数
React.js: onClick function from child to parent
我以 this 文章为例(React 方式),但它对我不起作用。请指出我的错误,因为我不明白哪里出了问题。
这是我看到的错误:
Uncaught TypeError: this.props.onClick is not a function
这是我的代码:
// PARENT
var SendDocModal = React.createClass({
getInitialState: function() {
return {tagList: []};
},
render: function() {
return (
<div>
{
this.state.tagList.map(function(item) {
return (
<TagItem nameProp={item.Name} idProp={item.Id} onClick={this.HandleRemove}/>
)
})
}
</div>
)
},
HandleRemove: function(c) {
console.log('On REMOVE = ', c);
}
});
// CHILD
var TagItem = React.createClass({
render: function() {
return (
<span className="react-tagsinput-tag">
<span>{this.props.nameProp}</span>
<a className='react-tagsinput-remove' onClick={this.HandleRemove}></a>
</span>
)
},
HandleRemove: function() {
this.props.onClick(this);
}
});
提前致谢!
问题是 map
回调中的 this
没有引用 React 组件,因此 this.HandleRemove
是 undefined
。
您可以通过将第二个参数传递给 map
来显式设置 this
值:
this.state.tagList.map(function() {...}, this);
现在this
inside回调指的是与this
outside回调相同的值,即SendDocModal
实例。
这与 React 无关,这只是 JavaScript 的工作方式。有关详细信息和其他解决方案,请参阅 How to access the correct `this` context inside a callback?。
尝试以下操作:
var SendDocModal = React.createClass({
getInitialState: function() {
var item = {};
item.Name = 'First';
item.Id = 123;
var item2 = {};
item2.Name = 'Second';
item2.Id = 123456;
return {tagList: [item,item2]};
},
HandleRemove: function(c){
console.log('On REMOVE = ', c);
},
render: function() {
return (<div>
{this.state.tagList.map(function(item){
return(
<TagItem nameProp={item.Name} idProp={item.Id} key={item.Id} click={this.HandleRemove}/>
)}, this)}
</div>
)
}
});
// CHILD
var TagItem = React.createClass({
handleClick: function(nameProp)
{
this.props.click(nameProp);
},
render: function(){
return(
<span className="react-tagsinput-tag" ><span onClick={this.handleClick.bind(this, this.props.nameProp)}>{this.props.nameProp}</span><a className='react-tagsinput-remove' ></a></span>
)
}
});
少量更改:
在 tagList 映射后添加了 'this'。老实说,我不完全确定为什么 - 也许更有经验的程序员可以告诉我们。
为每个 TagItem 添加了一个键。这是推荐的做法,控制台会通知您应该这样做,以便在状态发生变化时,React 可以相应地跟踪每个项目。
点击是通过道具传递的。参见 React js - having problems creating a todo list
我以 this 文章为例(React 方式),但它对我不起作用。请指出我的错误,因为我不明白哪里出了问题。
这是我看到的错误:
Uncaught TypeError: this.props.onClick is not a function
这是我的代码:
// PARENT
var SendDocModal = React.createClass({
getInitialState: function() {
return {tagList: []};
},
render: function() {
return (
<div>
{
this.state.tagList.map(function(item) {
return (
<TagItem nameProp={item.Name} idProp={item.Id} onClick={this.HandleRemove}/>
)
})
}
</div>
)
},
HandleRemove: function(c) {
console.log('On REMOVE = ', c);
}
});
// CHILD
var TagItem = React.createClass({
render: function() {
return (
<span className="react-tagsinput-tag">
<span>{this.props.nameProp}</span>
<a className='react-tagsinput-remove' onClick={this.HandleRemove}></a>
</span>
)
},
HandleRemove: function() {
this.props.onClick(this);
}
});
提前致谢!
问题是 map
回调中的 this
没有引用 React 组件,因此 this.HandleRemove
是 undefined
。
您可以通过将第二个参数传递给 map
来显式设置 this
值:
this.state.tagList.map(function() {...}, this);
现在this
inside回调指的是与this
outside回调相同的值,即SendDocModal
实例。
这与 React 无关,这只是 JavaScript 的工作方式。有关详细信息和其他解决方案,请参阅 How to access the correct `this` context inside a callback?。
尝试以下操作:
var SendDocModal = React.createClass({
getInitialState: function() {
var item = {};
item.Name = 'First';
item.Id = 123;
var item2 = {};
item2.Name = 'Second';
item2.Id = 123456;
return {tagList: [item,item2]};
},
HandleRemove: function(c){
console.log('On REMOVE = ', c);
},
render: function() {
return (<div>
{this.state.tagList.map(function(item){
return(
<TagItem nameProp={item.Name} idProp={item.Id} key={item.Id} click={this.HandleRemove}/>
)}, this)}
</div>
)
}
});
// CHILD
var TagItem = React.createClass({
handleClick: function(nameProp)
{
this.props.click(nameProp);
},
render: function(){
return(
<span className="react-tagsinput-tag" ><span onClick={this.handleClick.bind(this, this.props.nameProp)}>{this.props.nameProp}</span><a className='react-tagsinput-remove' ></a></span>
)
}
});
少量更改:
在 tagList 映射后添加了 'this'。老实说,我不完全确定为什么 - 也许更有经验的程序员可以告诉我们。
为每个 TagItem 添加了一个键。这是推荐的做法,控制台会通知您应该这样做,以便在状态发生变化时,React 可以相应地跟踪每个项目。
点击是通过道具传递的。参见 React js - having problems creating a todo list