简体   繁体   English

如何在 ReactJS 的单个事件处理程序中传递多个道具?

[英]How can I pass multiple props in a single event handler in ReactJS?

I'm trying to add a onClick listener to a li and the props will be passed to another component that will eventually call a function.我正在尝试将 onClick 侦听器添加到 li,并且道具将传递给最终将调用 function 的另一个组件。 My first thought was simply put 2 onClick, but I got a warning for duplicate props.我的第一个想法是简单地放 2 onClick,但我收到了重复道具的警告。

<li onClick={props.headlinesClicked} onClick={props.getData}>Top Headlines</li>

Then I tried the vahnilla JS way but I assume it's not working because these are expressions rather than actual functions.然后我尝试了 vahnilla JS 方式,但我认为它不起作用,因为这些是表达式而不是实际函数。

 <li onClick={() => {{props.headlinesClicked}; {props.getData}}}>Top Headlines</li>

How can I pass these multiple props in a single onClick?如何在单个 onClick 中传递这些多个道具?

Have the passed function call the other two functions normally: 让传递的函数正常调用其他两个函数:

<li onClick={() => { props.headlinesClicked(); props.getData() }}>Top Headlines</li>

Alternatively (recommended for readability), make a new function which calls those two functions. 或者(建议为了可读性),创建一个调用这两个函数的新函数。 Consider making the function a class method if it's a class component. 如果它是类组件,请考虑使该函数成为类方法。

const handleListItemClick = () => {
  props.headlinesClicked()
  props.getData()
}

return <li onClick={handleListItemClick}>Top Headlines</li>

Your first snippet won't work as you can't bind to a prop multiple times. 您的第一个片段无法正常工作,因为您无法多次绑定到道具。 The second is incorrect firstly because anything inside the brackets is JS, so the additional brackets inside are unnecessary and you're also not invoking any of the functions. 第二个是不正确的,因为括号内的任何东西都是JS,所以内部的附加括号是不必要的,你也没有调用任何函数。

Think of it this way, onClick={props.headlinesClicked} is kind of like a shorthand for onClick={() => props.headlinesClicked()} (not exactly as onClick also passes an event as the first parameter, but hopefully you get the gist of it). 想一想, onClick={props.headlinesClicked}有点像onClick={() => props.headlinesClicked()}的简写(不完全像onClick也传递一个事件作为第一个参数,但希望你得到它的要点)。 So, when you do onClick={() => props.headlinesClicked} you're not actually calling the function, just returning a reference to it which then doesn't actually do anything. 所以,当你执行onClick={() => props.headlinesClicked}你实际上并没有调用该函数,只是返回对它的引用,然后实际上什么也没做。

You could either do onClick={() => props.headlinesClicked(); props.getData()} 你可以做onClick={() => props.headlinesClicked(); props.getData()} onClick={() => props.headlinesClicked(); props.getData()} , or extract it to a new function/method that does both calls, then invoke it with onClick={myNewMethod} or onClick={() => myNewMethod()} . onClick={() => props.headlinesClicked(); props.getData()} ,或者将它提取到一个执行两个调用的新函数/方法,然后使用onClick={myNewMethod}onClick={() => myNewMethod()}调用它。

I have recommended below way it will work 100%我推荐了以下方式,它将 100% 工作

const handleListItemClick = () => {常量句柄ListItemClick = () => {

if(props.headlinesClicked){如果(道具.headlinesClicked){

props.headlinesClicked() props.headlinesClicked()

}else{ props.getData() } }else{ props.getData() }

return Top Headlines返回头条新闻

Note:react have confuse two props we mentioned same onClick.注意:react 混淆了我们提到的两个道具 onClick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM