Skip to content
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

feat: add useEventCallback() #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

feat: add useEventCallback() #6

wants to merge 5 commits into from

Conversation

RYOSKATE
Copy link

@RYOSKATE RYOSKATE commented May 3, 2020

facebook/react#14099 (comment)
Using this function, the following TypeScript error will occur at (0, ref.current).

$ tsc --build tsconfig.json
src/index.ts:10:38 - error TS2695: Left side of comma operator is unused and has no side effects.

10     return useCallback((...args) => (0, ref.current)(...args), []);

For now, change it to ref.current(....args) to avoid this error.

@artem-malko
Copy link

artem-malko commented Jun 4, 2020

Hi @exKAZUu , why did you change useLayoutEffect to useImperativeHandle?

@exKAZUu
Copy link
Member

exKAZUu commented Jun 5, 2020

Hi @artem-malko

If we use useLayoutEffect and the callback is outdated (facebook/react#14099 (comment)), it is difficult for us to recognize it.

But, if we use useImperativeHandle and ref.current is null (facebook/react#14099 (comment)), then we can recognize it due to the error where ref.current is null.

(Moreover, Btw, I think the case where ref.current is null is rare because I've never seen the code that the callback is called in useLayoutEffect().)

Note useImperativeHandle solves the problem of useLayoutEffect (facebook/react#14099 (comment)).

So, I believe that the problem of useLayoutEffect is worse than the problem of useImperativeHandle.

How do you think? I just want to employ a better solution. Please let me know your thought!

@artem-malko
Copy link

artem-malko commented Jun 5, 2020

@exKAZUu

I thought, the main problem of useLayoutEffect is facebook/react#14099 (comment) And devs from react-redux have solved that problem with useIsomorphicLayoutEffect.

And the last thing: calling from render: https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback There is a simple solution:

const ref = useRef(() => {
    throw new Error('Cannot call an event handler while rendering.');
  });

As for me, usage of useImperativeHandle in a such context is quite strange and not obviously. May be it is because I am new to this concept (I mean useImperativeHandle).

@exKAZUu
Copy link
Member

exKAZUu commented Jun 5, 2020

Thanks! Yeah, useIsomorphicLayoutEffect seems better than useLayoutEffect.
I also agree that useImperativeHandle seems strange. Acutally, I still cannot fully understand the behavior of useImperativeHandle.

I just tested ypresto's example (facebook/react#14099 (comment)).
In the case where the callback is called from useLayoutEffect in a descendant component, useImperativeHandle causes an error, but useLayoutEffect works silently wrongly. So, I chose useImperativeHandle.
(I believe there is no other difference between useImperativeHandle and useLayoutEffect (useIsomorphicLayoutEffect).)

@artem-malko
Copy link

@exKAZUu we need a useImperativeHandle specialist here)

If you will add

const ref = useRef(() => {
    throw new Error('Cannot call an event handler while rendering.');
  });

to useLayoutEventCallback, you will see an error: https://codesandbox.io/s/react-useeventcallback-test-mblch

So, it is not silently wrongly now) What do you think?

@exKAZUu
Copy link
Member

exKAZUu commented Jun 5, 2020

Sorry, I missed your sample code 😓 I feel your are right. I will test some code including yours and reply.

@artem-malko
Copy link

@exKAZUu cool)

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

Successfully merging this pull request may close these issues.

3 participants