Skip to content

Do not work, If we pass element as ref #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
prathameshmhatre opened this issue Nov 9, 2020 · 9 comments
Closed

Do not work, If we pass element as ref #35

prathameshmhatre opened this issue Nov 9, 2020 · 9 comments

Comments

@prathameshmhatre
Copy link

If we pass element ref as below

const counterEl = useRef(null);

useEventListener('click', () => alert('Ref click'), counterEl);

  return (
    <div className="App">
      Count: <span ref={counterEl}>{count}</span>
      <button onClick={increment}>+</button>
    </div>
  );

But, It will work If we pass elementRef.current as below
useEventListener('click', () => alert('Ref click'), counterEl.current);

Will suggest a change, and If you allow will create a PR with the following change

    const targetIsRef = element.hasOwnProperty('current');
    const currentTarget = targetIsRef ? element.current : element;
    const isSupported = currentTarget && currentTarget.addEventListener;
@srmagura
Copy link
Collaborator

srmagura commented Nov 9, 2020

I prefer the current design but let's see what others say.

@prathameshmhatre
Copy link
Author

Any update guys?

@donavon
Copy link
Owner

donavon commented Nov 19, 2020

I like this. It adds support without breaking existing code. PRs welcome.

@donavon
Copy link
Owner

donavon commented Nov 19, 2020

after some thought, what exactly is your use case and why can't you solve this with built-in React onClick (or other) handers?

@jarnaiz
Copy link

jarnaiz commented Nov 23, 2020

Hello,
I have the exact same issue, I am using a third party library https://www.telerik.com/kendo-react-ui/components/inputs/checkbox/ and this component does not expose the onClick event.

Thanks!

@prathameshmhatre
Copy link
Author

Team, I have the code ready but the test case is giving 90%. Can someone help me. I have attached the test result. Please find the below code that I have added

    let targetIsRef = false;
    if (element) {
      targetIsRef = Object.prototype.hasOwnProperty.call(element, 'current');
    }
    const currentTarget = targetIsRef ? element.current : element;
    const isSupported = currentTarget && currentTarget.addEventListener;
    if (!isSupported) {
      return;
    }

index js — use-event-listener 2020-11-26 17-41-53

@donavon
Copy link
Owner

donavon commented Nov 27, 2020

@prathameshmhatre @jarnaiz

I was in the middle of a rewrite in TypeScript anyway, so I went ahead and added ref support. See #36

Plz try it out (published tp npm as @use-it/[email protected]) and let me know if this works for you.

@iamaamir
Copy link

iamaamir commented Mar 9, 2021

@prathameshmhatre @jarnaiz

I was in the middle of a rewrite in TypeScript anyway, so I went ahead and added ref support. See #36

Plz try it out (published tp npm as @use-it/[email protected]) and let me know if this works for you.

check #40

@FoxxMD
Copy link

FoxxMD commented Apr 15, 2021

Any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants