简体   繁体   English

如何将函数和道具都传递给 React 中的 onClick 事件?

[英]How to pass both a Function and Prop to an onClick Event in React?

I only see examples of people passing two functions but nothing on a prop and function simultaneously.我只看到人们传递两个函数但没有同时传递道具和函数的例子。 Let's say I want the pass onClick={props.tested} (prop) and onClick={divTested} (function) at the same time.假设我想要同时传递onClick={props.tested} (prop) 和onClick={divTested} (function)。 How do I do this??我该怎么做呢??

Although I rarely see this done, you can indeed add properties to a function, and pass that "function with properties" into another function that is aware of those properties:虽然我很少看到这样做,但您确实可以向函数添加属性,并将“具有属性的函数”传递给另一个知道这些属性的函数:

 let functionWithProperties = function(a, b) { return a + b; } // Add Properties to the function: functionWithProperties.a = 5; functionWithProperties.b = 10; function doSomethingWithFunctionWithProperties(func) { return func(func.a,func.b); } // Pass function with properties into another function // that is aware of those properties: let output = doSomethingWithFunctionWithProperties(functionWithProperties); console.log(output); // 15

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

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