-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
InputText: lack of the input reference when the useLayoutEffect is being executed #7525
Comments
I never use https://stackblitz.com/edit/cbt4me2u-saueapxw?file=src%2FApp.jsx |
You should generally prefer useEffect over useLayoutEffect in React for the following reasons: • Synchronous Execution: useLayoutEffect runs synchronously, meaning it blocks the browser's painting process until the effect is complete. This can lead to performance issues, especially if the effect contains heavy computations or DOM manipulations. Server-Side Rendering (SSR): • SSR Incompatibility: useLayoutEffect does not work with SSR, as it relies on browser-specific APIs that are not available on the server. If you are using SSR, you should use useEffect instead. Use Cases for useEffect: • Data Fetching: Fetching data from APIs or other external sources. When to Use useLayoutEffect: • DOM Measurements: If you need to measure the layout of a DOM element before the next paint, for example, to position a tooltip or dynamically adjust element sizes. Rule of Thumb: • If your effect does not require immediate synchronization with the DOM and does not involve heavy computations, use useEffect. |
@melloware I'm not sure why comparing useEffect to useLayoutEffect here as for the regular useEffect the problem also exists. It is not relevant in the context of the bug. But thx for providing the workaround |
I checked the provided workaround and unfortunately, for the production build it's not working. @melloware pls reopen this bug. |
@mikejav are you sure? Try this: https://stackblitz.com/edit/cbt4me2u-saueapxw?file=src%2Fmain.jsx,src%2FApp.jsx I removed |
@melloware I'm checking it in my application in the production build. Maybe it doesn't work because of some additional compile-time optimizations and is not related to the strict mode 🤔 Workaround: I wrapped the code in the useLayoutEffect into the setTimeout but it's a dirty solution unfortunately |
Weird there must be something else going on in your code. Because Production Mode typically means it turns Strict Mode off so hooks fire once instead of twice. |
Describe the bug
Setting the
ref
to the element doesn't work correctly.If you create a new ref, assign it to the InputText component, and try to focus the input, then the error will be thrown because the element reference is not set yet.
Note: adding the
?
operator will fix the issue in the dev environment (React's StrictMode) but it won't for the production build. On the production build the useLayoutEffect will be called only once when the ref is not set yet.Reproducer
https://stackblitz.com/edit/cbt4me2u-y1z4u7vw?file=src%2FApp.jsx
System Information
Steps to reproduce the behavior
Expected behavior
The element's reference should be set already when executing the useLayoutEffect
The text was updated successfully, but these errors were encountered: