简体   繁体   English

React JS:调用事件处理程序

[英]React JS: calling event handlers

I have started working in React recently.我最近开始在 React 中工作。 I noticed one behaviour like when I am trying to call my event handle with the same component like this onClick={someEventHandler} it is triggering but when I try to do the same same like this onClick={()=> someEventHandler} it doesn't work.我注意到一种行为,例如当我尝试使用类似 onClick={someEventHandler} 的相同组件调用事件句柄时,它正在触发,但是当我尝试像这样 onClick={()=> someEventHandler} 执行相同操作时,它不会不行。

I noticed that when I need to pass any arguments and calling the function like onClick={()=>someEventHandler(id)} it is working fine.我注意到,当我需要传递任何 arguments 并像 onClick={()=>someEventHandler(id)} 一样调用 function 时,它工作正常。

can some one explain me the logic/theory behind this?有人可以解释一下这背后的逻辑/理论吗?

The onClick event handler needs a function to be passed to it. onClick 事件处理程序需要将 function 传递给它。 Whenn the event is triggered, it calls the handler function passed to it.当事件被触发时,它会调用传递给它的处理程序 function。

In the first case it works because you pass someEventHandler to onClick which is invoked when event is fired.在第一种情况下,它可以工作,因为您将someEventHandler传递给 onClick ,它在事件触发时被调用。 An invocation to the function is like someEventHandler()对 function 的调用就像someEventHandler()

Now in the second case,现在在第二种情况下,

onClick={()=>someEventHandler}

the function passed to onClick is ()=>someEventHandler which can be elaborated further as ()=> { return someEventHandler; }传递给 onClick 的 function 是()=>someEventHandler可以进一步阐述为()=> { return someEventHandler; } ()=> { return someEventHandler; }

Now if you notice above you are returning a function from the onClick event handler.现在,如果您注意到上面的内容,您将从 onClick 事件处理程序返回 function。 The returned function is now never invoked and hence you see the above behavior返回的 function 现在永远不会被调用,因此您会看到上述行为

It works in the last case like onClick={()=>someEventHandler(id)} , because when the event handler is invoked, it invoked someEventHandler with id too.它适用于最后一种情况,例如onClick={()=>someEventHandler(id)} ,因为当调用事件处理程序时,它也调用了带有 id 的 someEventHandler。

However you do not need to pass id to invoke it, you can simply use it like onClick={()=>someEventHandler()} and it work, provided you don't need id as a parameter in someEventHandler.但是,您不需要传递 id 来调用它,您可以像onClick={()=>someEventHandler()}一样简单地使用它,并且它可以工作,前提是您不需要 id 作为 someEventHandler 中的参数。

Another thing to note when you are using function like onClick={()=>someEventHandler()} instead of onClick={someEventHandler} is that your someEventHandler will not be invoked with any arguments.当您使用 function(如onClick={()=>someEventHandler()}而不是onClick={someEventHandler}时,需要注意的另一件事是您的someEventHandler不会被任何 arguments 调用。 If you want the event to be passed as argument to someEventHandler, you need tto explicitly pass it like如果您希望将事件作为参数传递给 someEventHandler,则需要像这样显式传递它

onClick={(e)=>someEventHandler(e)}

onClick={someEventHandler} will trigger the handler as soon as your component gets loaded/rendered on the screen but by adding an arrow function before the handler like this - onClick={()=> someEventHandler} will make sure not to trigger the component before you click on it onClick={someEventHandler}将在您的组件在屏幕上加载/渲染后立即触发处理程序,但是通过在这样的处理程序之前添加箭头 function - onClick={()=> someEventHandler}将确保不触发组件在你点击它之前

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

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