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
useCallback returns undefined when the component is rendered server-side #14241
Comments
I presume that the rationale is that since callbacks are for usage with event handlers such as Don't worry, when rehydrating, the callbacks will be there. But, may I ask what exactly are you trying to do? |
Thanks @arianon 👌 I was following redux-react-hook's suggestion to use
I'll open an issue on the docs repository to suggest an update 😉 Thanks again! |
I believe this is a bug; that code comment is wrong. My understanding is it is correct to use useCallback for render phase callbacks. |
AFAICT the only things that should not run server side are Effects and ref attribute initialization. A working useCallback is important for, for example, the render prop HOC pattern, which will still be with the ecosystem for a long time. |
Do you want to request a feature or report a bug?
Bug.
What is the current behavior?
React.useCallback
returnsundefined
when the component is rendered server-side.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
This bug can be reproduced by creating a React component using a
useCallback
. Logging the return value on the server yieldsundefined
while it properly returns the function in the browser.You can observe this bug in this CodeSandbox: https://codesandbox.io/s/r557kww6wn The application is properly rendered to a string bug looking at the console, you will see that the return value of
useCallback
isundefined
.Here's the exact same application but rendered on the client: https://codesandbox.io/s/xvwv797pxz The function is properly logged to the console.
What is the expected behavior?
React.useCallback
should return the memoized callback, notundefined
.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
The text was updated successfully, but these errors were encountered: