在反应中传递函数 onClick 时出错
Error passing function onClick in react
我有两个文件。一个列表组件和一个单项组件。在我的应用中,用户可以 select 多个项目。然后我在 "list" "items" 中创建一个状态元素,我的想法是当用户单击项目按钮时,列表元素通知列表组件并将项目保存在 [=24 的 Items 数组中=].
我有下一个代码
List.jsx:
registrarItems(data,item){
console.log(data,"aqui 1 con",item);
let items = this.state.itemsAgregados.slice();
if(!items.indexOf(data.id_producto)){
console.log("no se encuentra");
items.push(id);
this.setState({
'itemsAgregados':items
});
}else{
console.log("ya existe");
item.removerSeleccion();
}
console.log("registrando items",id);
}
render() {
return (
<div className="content-app">
<Navbar data={this.menu}/>
<div className="container lista-productos">
{
this.state.productos.map((producto, index) => {
return (
<Item data={producto}
registro = {this.registrarItems}
key={producto.id_producto}/>
);
})
}
</div>
</div>
);
}
和Item.jsx:
render() {
let props = this.props;
let img = JSON.parse(props.data.imagen);
let imgPath = Rutas.apiStatic + 'img/productos/' + props.data.id_producto + '/' + img.sm;
props.data.items = this;
return (
<div className="row item-listado">
<div className="col-xs-3">
<img src={imgPath} className="img-circle img-item"/>
</div>
<div className="col-xs-7">
<Link to={Rutas.producto + props.data.identificador}>
<h3 className="titulo">{props.data.titulo}</h3>
<span className="price">$ {props.data.precio}</span>
</Link>
</div>
<div className="col-xs-2 text-right">
<ul className="list-unstyled list-acciones">
<li>
<a href="#" onClick={()=>props.registro(props.data,this)} className={this.state.classAdd}>
<i className="fa fa-plus"></i>
</a>
</li>
</ul>
</div>
</div>
)
}
如您所见,我将 "registrarItems" 方法作为参数传递给 Item,然后我将其作为 onClick 事件添加到 item 的标签中。但是我需要将 "data" 和自己的 "item" 元素传递给 onclick 函数。第一个,用于保存在 Items 数组中单击的元素,或将其删除(如果它已经存在),因为按钮可能具有切换功能。但是在我的 "console.log" 中,通过箭头函数 传递给 onClick 方法的两个参数都显示为 "undefined"。
我看到了一些例子,但我没有得到我的错误。有谁能够帮助我?谢谢。
最终解决这个问题很简单。我用类似于 Free-soul 在他的评论中所说的内容解决了它。
首先,我将列表组件作为参数传递给项目。在我的 List 渲染方法代码下方:
{
this.state.productos.map((producto, index) => {
this.items[producto.id_producto] = producto;
return (
<Item data={producto}
parent = {this}
key={producto.id_producto}/>
);
})
}
然后我在 componentDidMount 方法中获取父参数,然后我直接从 List 方法调用 validarItem 函数,然后我将我需要的参数。
这是我的商品代码:
onClickPlus(id,data) {
//{Rutas.listas + 'productos/' + props.data.id_producto //Url para pasar uno solo
this.setState({
classAdd:'selected'
})
if(!this.state.parent.validarItem(this.state.data)){
this.removerSeleccion()
}
if(this.state.seleccionMultiple){
}
}
removerSeleccion(){
this.setState({classAdd:'normal'})
}
componentDidMount(){
this.setState({
parent: this.props.parent,
data : this.props.data
})
}
render() {
return (
// more code
<a href="#" onClick={() => this.onClickPlus(parent)} className={this.state.classAdd}>
<i className="fa fa-plus"></i>
</a>
//more render code...
)
}
我不知道这是否是最佳做法,但对我有用。
我有两个文件。一个列表组件和一个单项组件。在我的应用中,用户可以 select 多个项目。然后我在 "list" "items" 中创建一个状态元素,我的想法是当用户单击项目按钮时,列表元素通知列表组件并将项目保存在 [=24 的 Items 数组中=].
我有下一个代码
List.jsx:
registrarItems(data,item){
console.log(data,"aqui 1 con",item);
let items = this.state.itemsAgregados.slice();
if(!items.indexOf(data.id_producto)){
console.log("no se encuentra");
items.push(id);
this.setState({
'itemsAgregados':items
});
}else{
console.log("ya existe");
item.removerSeleccion();
}
console.log("registrando items",id);
}
render() {
return (
<div className="content-app">
<Navbar data={this.menu}/>
<div className="container lista-productos">
{
this.state.productos.map((producto, index) => {
return (
<Item data={producto}
registro = {this.registrarItems}
key={producto.id_producto}/>
);
})
}
</div>
</div>
);
}
和Item.jsx:
render() {
let props = this.props;
let img = JSON.parse(props.data.imagen);
let imgPath = Rutas.apiStatic + 'img/productos/' + props.data.id_producto + '/' + img.sm;
props.data.items = this;
return (
<div className="row item-listado">
<div className="col-xs-3">
<img src={imgPath} className="img-circle img-item"/>
</div>
<div className="col-xs-7">
<Link to={Rutas.producto + props.data.identificador}>
<h3 className="titulo">{props.data.titulo}</h3>
<span className="price">$ {props.data.precio}</span>
</Link>
</div>
<div className="col-xs-2 text-right">
<ul className="list-unstyled list-acciones">
<li>
<a href="#" onClick={()=>props.registro(props.data,this)} className={this.state.classAdd}>
<i className="fa fa-plus"></i>
</a>
</li>
</ul>
</div>
</div>
)
}
如您所见,我将 "registrarItems" 方法作为参数传递给 Item,然后我将其作为 onClick 事件添加到 item 的标签中。但是我需要将 "data" 和自己的 "item" 元素传递给 onclick 函数。第一个,用于保存在 Items 数组中单击的元素,或将其删除(如果它已经存在),因为按钮可能具有切换功能。但是在我的 "console.log" 中,通过箭头函数 传递给 onClick 方法的两个参数都显示为 "undefined"。 我看到了一些例子,但我没有得到我的错误。有谁能够帮助我?谢谢。
最终解决这个问题很简单。我用类似于 Free-soul 在他的评论中所说的内容解决了它。
首先,我将列表组件作为参数传递给项目。在我的 List 渲染方法代码下方:
{
this.state.productos.map((producto, index) => {
this.items[producto.id_producto] = producto;
return (
<Item data={producto}
parent = {this}
key={producto.id_producto}/>
);
})
}
然后我在 componentDidMount 方法中获取父参数,然后我直接从 List 方法调用 validarItem 函数,然后我将我需要的参数。 这是我的商品代码:
onClickPlus(id,data) {
//{Rutas.listas + 'productos/' + props.data.id_producto //Url para pasar uno solo
this.setState({
classAdd:'selected'
})
if(!this.state.parent.validarItem(this.state.data)){
this.removerSeleccion()
}
if(this.state.seleccionMultiple){
}
}
removerSeleccion(){
this.setState({classAdd:'normal'})
}
componentDidMount(){
this.setState({
parent: this.props.parent,
data : this.props.data
})
}
render() {
return (
// more code
<a href="#" onClick={() => this.onClickPlus(parent)} className={this.state.classAdd}>
<i className="fa fa-plus"></i>
</a>
//more render code...
)
}
我不知道这是否是最佳做法,但对我有用。