简体   繁体   English

在 React 中覆盖 onClick

[英]Overriding onClick in React

This is somewhat of weird question.这是一个有点奇怪的问题。 I'm working with event types in React, and we want to use onClick in some instances, and onPointerDownCapture in others (for reasons).我在 React 中处理事件类型,我们希望在某些情况下使用 onClick,在其他情况下使用 onPointerDownCapture(出于某些原因)。 But to make this more general, it could be any two different click-like events.但为了更一般化,它可以是任何两个不同的类点击事件。 The issue is that while we can assign whatever function handler on the right side of the expression, the left side has to be static, essentially.问题是虽然我们可以在表达式的右侧分配任何 function 处理程序,但左侧必须是 static,本质上。 So,所以,

<button onClick={handler} vs onPointerDownCapture={handler} vs onMouseDown={handler} /> <button onClick={handler} vs onPointerDownCapture={handler} } vs onMouseDown={ onMouseDown={handler} />

I think just using onPointerDownCapture will be fine for most usecases, but in a perfect world, I'd be able to flip between these at runtime based on other variables.我认为只使用 onPointerDownCapture 对于大多数用例来说都很好,但在一个完美的世界中,我可以在运行时根据其他变量在这些之间切换。 Is it possible to override the onClick on the button/div/whatever prototype or something to be whatever event type I want it to be?是否可以覆盖按钮/div/任何原型或其他东西上的 onClick 以成为我想要的任何事件类型?

Much googling.很多谷歌搜索。 No success.没有成功。

I didn't fully understand what you mean by “overriding onClick”, but我不完全理解你所说的“重写 onClick”是什么意思,但是

The issue is that while we can assign whatever function handler on the right side of the expression, the left side has to be static, essentially.问题是虽然我们可以在表达式的右侧分配任何 function 处理程序,但左侧必须是 static,本质上。

This is not true, left hand side could be dynamic, here's how:这不是真的,左侧可能是动态的,方法如下:

<button {...({ [eventName]: handler })} />

I guess this solves your problem.我想这可以解决您的问题。


Ok above syntax is a bit terse and admittedly confusing.好的,上面的语法有点简洁,而且确实令人困惑。 It's the good old JSX spread props syntax, just over an inline object literal.这是很好的旧 JSX 传播道具语法,就在内联 object 文字之上。

I'll give you another equivalent form, hopefully it should be more readable.我会给你另一个等效的形式,希望它应该更具可读性。

const eventName = someCondition ? "onPointerDownCapture" : "onClick"

const props = { 
  [eventName]: handler
}

<button {...props} />

You have to use those attribute names and you use the same function name for all 3 of them.您必须使用这些属性名称,并且对所有 3 个属性名称使用相同的 function 名称。

What these 3 attributes do is they register the associated event.这 3 个属性的作用是注册关联的事件。

Maybe you could use a useEffect and add there conditionally an event listener instead of the proposed React attributes.也许你可以使用useEffect并有条件地添加一个事件监听器而不是建议的 React 属性。

I think best is @vera solution in comment.我认为最好的是评论中的@vera解决方案。 Pass extra prop to component (for example isOnClick), and based on it pass either callback or undefined to event handler prop:将额外的属性传递给组件(例如 isOnClick),并基于它传递回调或未定义到事件处理程序属性:

function Component(props: { isOnClick: boolean; callback: () => void }) {
  return (
    <div
      onClick={props.isOnClick ? props.callback : undefined}
      onMouseDown={props.isOnClick ? undefined : props.callback}
    />
  );
}

Note that passing undefined to prop is same as not setting that prop.请注意,将 undefined 传递给 prop 与不设置该 prop 相同。

Alternatively conditionaly return component:或者有条件地返回组件:

function Component(props: { isOnClick: boolean; callback: () => void }) {
  if (props.isOnClick) {
    return <div onClick={props.callback}/>
  } else {
    return <div onMouseDown={props.callback}/>
  };
}

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

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