Skip to content

[useClickAway] 支持传入事件监听选项 #2770

@Xav1erSue

Description

@Xav1erSue

背景:如果有元素的点击事件中阻止了冒泡,会导致 useClickAway 无法捕捉到此次点击

代码:

import React, { useState, useRef } from "react";
import { useClickAway } from "ahooks";

export default () => {
  const [counter, setCounter] = useState(0);

  const ref = useRef<HTMLButtonElement>(null);

  useClickAway(() => setCounter((s) => s + 1), ref);

  return (
    <div>
      <button ref={ref} type="button">
        box1
      </button>
      <button
        type="button"
        style={{ marginLeft: 16 }}
        onClick={(e) => e.stopPropagation()}
      >
        box2
      </button>
      <p>counter: {counter}</p>
    </div>
  );
};

期望:支持传入事件监听选项如 capture 等参数,改变事件捕获时机

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions