[英]ReactJS - error passing an argument to a function
I've got the following Reactjs code. 我有以下Reactjs代码。 It generates an inline menu (list of elements).
它生成一个内联菜单(元素列表)。 If I click on a particular element, I'd like it to be output in console but it does not work.
如果我单击一个特定的元素,我希望将其输出到控制台中,但它不起作用。
import React from "react";
export default class GalleryHeader extends React.Component {
handleClick(cat) {
console.log(cat);
}
createItems(items) {
var output = [];
for(var i = 0; i < items.length; i++)
output.push(<li onClick={this.handleClick({items[i]})}>{items[i]}</li>);
return output;
}
render() {
return (
<ul class="list-inline">
{this.createItems(this.props.menuItems)}
</ul>
);
}
}
There seems to be an error in calling the function 调用该函数似乎出错
{this.handleClick({items[i]})}
When I remove the function argument like below, it works fine: 当我删除如下所示的函数参数时,它可以正常工作:
handleClick() {
console.log("test");
}
createItems(items) {
var output = [];
for(var i = 0; i < items.length; i++)
output.push(<li onClick={this.handleClick}>{items[i]}</li>);
Please advise. 请指教。
It's not a ReactJS error, it's a simple JS error. 这不是ReactJS错误,而是一个简单的JS错误。
You need to pass the function reference to the onClick
event. 您需要将函数引用传递给
onClick
事件。 When you do <li onClick={this.handleClick({items[i]})}>{items[i]}</li>
you're executing that function and passing its result to the event. 当您执行
<li onClick={this.handleClick({items[i]})}>{items[i]}</li>
您正在执行该函数并将其结果传递给事件。
The correct call would be something like 正确的电话是这样的
<li onClick={this.handleClick.bind(this, items[i])}>{items[i]}</li>
That way your this
scope doesn't change and you can also receive params. 这样,你的
this
范围不会改变,你也可以得到PARAMS。
Another solution would be using ES6 arrow function as: 另一个解决方案是将ES6箭头功能用作:
<li onClick={() => this.handleClick(items[i])}>{items[i]}</li>
What's the difference here? 这有什么区别?
Well, now you're passing a function reference (as () => {}
implies) that will call your this.handleClick()
afterwards. 好吧,现在您正在传递一个函数引用(如
() => {}
所暗示),该函数引用随后将调用this.handleClick()
。
Tips not related to your question
与您的问题无关的提示
Firstly, you should be using className
instead of class
when using JSX syntax. 首先,在使用JSX语法时,应该使用
className
而不是class
。 That's easy to forget! 这很容易忘记!
Secondly, it's recommended to use a key
attribute whenever you're rendering elements withing a loop. 其次,建议每当通过循环渲染元素时都使用
key
属性。 So your <li>
s would be: 因此,您的
<li>
是:
<li key={i} onClick={() => this.handleClick(items[i])}>{items[i]}</li>
This way React can optimize its rendering and stop giving you warning/errors on your browser console. 这样,React可以优化其渲染,并在浏览器控制台上停止向您发送警告/错误。
You can take further reading into keys in React docs: Fragment , Multiple Components and Reconciliation 您可以进一步阅读React文档中的键: Fragment , Multiple Components和Reconciliation
In your code there are several mistakes 在您的代码中有几个错误
You should pass to onClick
reference to function, but you call it and result is passing to onClick
- but result is undefined
( onClick={ undefined }
) that's why click does not work. 您应该将
onClick
引用传递给函数,但您将其调用,结果传递给onClick
但结果undefined
( onClick={ undefined }
),这就是单击不起作用的原因。 To solve this problem you can use .bind
要解决此问题,您可以使用
.bind
onClick={ this.handleClick.bind(this, items[i]) }
or arrow function
或
arrow function
onClick={ () => this.handleClick(items[i]) }
In React to set class attribute you should use className
attribute instead of class
在React设置class属性中,您应该使用
className
属性而不是class
<ul className="list-inline">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.