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

Seems like useDebouncedCallback is executing async functions in render scope #139

Closed
dzearing opened this issue Jul 28, 2022 · 2 comments
Closed
Labels
question Further information is requested

Comments

@dzearing
Copy link

dzearing commented Jul 28, 2022

Describe the bug
I'm looking through the source here, and noticed something you may want to consider.

When you call useDebouncedCallback in a server environment using ReactDOM.renderToString, no async calls to requestAnimationFrame or setTimeout should occur. React will avoid calling useEffect mounting code to enable this case.

Unforunately, this hook is calling setTimeout in render scope. This should be refactored to only perform side effects like enquing async callbacks in useEffect, where you can avoid them in server scenarios and safely cancel them in client scenarios.

To Reproduce

const Component = () => {
   const cb = useDebouncedCallback(() => console.log('nooo'), 1000);
   cb();

  return </>;
});

render this to a string in a browser using React.renderToString, and see the message in the console 1 second later.

https://codesandbox.io/s/proud-lake-vjwqh5?file=/src/index.tsx

Expected behavior
No message in the console

use-debounce version:
reading the github code, but also 8.0.3 in the sandbox.

@xnimorz
Copy link
Owner

xnimorz commented Aug 13, 2022

Hey @dzearing

Thank you for the report!
I'm curious, what is the case to call the callback in the render scope?

As it's an uncommon case.

@xnimorz xnimorz added the question Further information is requested label Oct 15, 2022
@xnimorz
Copy link
Owner

xnimorz commented Nov 5, 2023

@xnimorz xnimorz closed this as completed Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants