简体   繁体   English

React JSX 中的 onClick={increase} 和 onClick={()=>{increase()}} 有什么区别?

[英]What is the difference between onClick={increase} and onClick={()=>{increase()}} in React JSX?

I did a search and didn't find any results, so I'm asking here.我搜索了一下,没有找到任何结果,所以我在这里问。

If the increase function takes no arguments, there is no difference.如果increase function 没有 arguments,则没有区别。

The only way there would be a difference would be if the component in which this prop is defined used the value returned from inside the onClick function.唯一不同的方法是,如果定义此道具的组件使用从onClick function 内部返回的值。 For example, for the general case:例如,对于一般情况:

<Foo fn={fn} />
<Foo fn={() => { fn() }} />

Those two are not equivalent, because the first will return the return value of fn to Foo, and the second will return undefined to Foo.这两个是不等价的,因为第一个会将fn的返回值返回给 Foo,而第二个会将undefined返回给 Foo。 If Foo uses the return value of its fn prop, you'll see a difference.如果 Foo 使用它的fn属性的返回值,你会看到不同。

But for the onClick prop in React, the return value doesn't matter , and is ignored - so you may as well use onClick={increase} to save on a few characters.但是对于 React 中的onClick返回值无关紧要,并且被忽略 - 所以你最好使用onClick={increase}来节省几个字符。

If the increase function does take arguments, then doing如果increase function确实取arguments,那么做

onClick={increase}

will pass along the click event to the function, but将点击事件传递给 function,但是

onClick={() => { increase() }}

will not pass anything to the function.不会将任何东西传递给 function。

(to pass the event to the function with the anonymous inline form, you'd need onClick={(e) => { increase(e) }} ) (要使用匿名内联表单将事件传递给 function,您需要onClick={(e) => { increase(e) }}

One addition to CertainPerformance 's answer - Using onClick={increase} , we can pass a memoized version of the callback by passing the function to useCallback hook.CertainPerformance的回答的一个补充 - 使用onClick={increase} ,我们可以通过将 function 传递给useCallback挂钩来传递回调的记忆版本。

Like,喜欢,

const increase = useCallback(()=>{
   //some logic
},[])
...

<Foo onClick={increase} />

This helps in preventing unnecessary rendering of components on re-render of parent and thereby increasing performance.这有助于防止在重新渲染父组件时不必要地渲染组件,从而提高性能。

While in the case of onClick = {()=>{increase()}} , re-rendering of the component will occur every time as new reference to the callback is created on every render of the parent component.而在onClick = {()=>{increase()}}的情况下,每次都会重新渲染组件,因为在每次渲染父组件时都会创建对回调的新引用。 Also, we cannot use useCallback here like onClick = {useCallback(()=>{increase()},[])} as useCallback cannot be called inside a callback.此外,我们不能像onClick = {useCallback(()=>{increase()},[])}在这里使用useCallback ,因为不能在回调中调用useCallback We get the error - React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.我们收到错误消息 - React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function. React Hook "useCallback" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function.

Refer: Rules of hooks , useCallback参考:钩子规则useCallback

the difference is that:不同之处在于:

onClick={increase}

only called the "increase" function, you can not pass an argument to "increase" function by this method, and also you can call only this one function, but when you use:仅调用“增加”function,您不能通过此方法将参数传递给“增加”function,您也只能调用这个ZC1C425268E68385D14AB5074C17Z,但是当您使用:

onClick={()=> {
increase(argument1, argument2,...);
secondFunction();
thirdFunction();
//and so many other functions
}
}

you can pass arguments to your functions, and also you can call other functions as well.您可以将 arguments 传递给您的函数,也可以调用其他函数。

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

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