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

InputText: lack of the input reference when the useLayoutEffect is being executed #7525

Closed
mikejav opened this issue Dec 24, 2024 · 2 comments
Labels
Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team

Comments

@mikejav
Copy link

mikejav commented Dec 24, 2024

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

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
    Memory: 13.50 GB / 31.93 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\NodeJS 20.10.0\node.EXE
    npm: 10.2.3 - C:\Program Files\NodeJS 20.10.0\npm.CMD
  Browsers:
    Edge: Chromium (131.0.2903.112)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    primereact: ^10.8.5 => 10.8.5

Steps to reproduce the behavior

  1. Go to https://stackblitz.com/edit/cbt4me2u-y1z4u7vw?file=src%2FApp.jsx
  2. You can see an Error thrown immediately in the console
  3. Compare it with the regular HTML input: https://stackblitz.com/edit/cbt4me2u?file=src%2FApp.jsx

Expected behavior

The element's reference should be set already when executing the useLayoutEffect

@mikejav mikejav added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 24, 2024
@melloware
Copy link
Member

I never use useLayoutEffect but it does work if I use useMountEffect

https://stackblitz.com/edit/cbt4me2u-saueapxw?file=src%2FApp.jsx

@melloware melloware added Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Dec 30, 2024
@melloware
Copy link
Member

melloware commented Dec 30, 2024

You should generally prefer useEffect over useLayoutEffect in React for the following reasons:
Performance:

• 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.
• Asynchronous Execution: useEffect, on the other hand, runs asynchronously after the render cycle, allowing the browser to paint the screen first. This results in a smoother user experience, particularly in complex applications.

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.
• Subscriptions: Setting up and tearing down subscriptions to external data sources.
• Manipulating the DOM (non-critical): Updating the DOM in a way that does not require immediate synchronization with the screen.

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.
• Preventing Visual Flickering: In rare cases where you need to ensure that a certain visual effect does not flicker, you can use useLayoutEffect to synchronize updates with the browser's painting process.

Rule of Thumb:

• If your effect does not require immediate synchronization with the DOM and does not involve heavy computations, use useEffect.
• If you need to interact with the DOM in a way that affects layout or to prevent visual flickers, use useLayoutEffect sparingly and with caution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team
Projects
None yet
Development

No branches or pull requests

2 participants