question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[React DOM Interactions] useListNavigation onNavigate not called in production builds

See original GitHub issue

We came across a bug in our application that uses @floating-ui/react-dom-interactions@0.11.0 and the useListNavigation hook not calling the provided onNavigate callback function but only in production builds.

Our application is bundled/built with Vite.

We found in production builds that keyboard events were no longer working for lists created by useListNavigation. I spent hours debugging our production builds attempting to find the culprit.

It appears to be something related to the usage of the useEvent hook. I’m not exactly sure what it is about that hook that is causing the problem, but the conditional useInsertionEffect and the check for the existence of that hook look suspicious at first glance.

We ended up patching useListNavigation in our app as follows which clears the issue up and gets things working again:

--- a/dist/floating-ui.react-dom-interactions.esm.js
+++ b/dist/floating-ui.react-dom-interactions.esm.js
@@ -2286,8 +2286,8 @@ const useListNavigation = function (_ref, _temp2) {
   const parentId = useFloatingParentNodeId();
   const tree = useFloatingTree();
   const previousOpen = usePrevious(open);
-  const onNavigate = useEvent(unstable_onNavigate);
-  const previousOnNavigate = useEvent(usePrevious(unstable_onNavigate));
+  const onNavigate = unstable_onNavigate;
+  const previousOnNavigate = usePrevious(unstable_onNavigate);
   const focusItemOnOpenRef = React.useRef(focusItemOnOpen);
   const indexRef = React.useRef(selectedIndex != null ? selectedIndex : -1);
   const keyRef = React.useRef(null);

I’d happily provide a PR to refactor the useEvent usage, but it’s not clear to me why this hook exists in the first place. It seems to me to be unnecessary. It’s not clear why useInsertionEffect would even be necessary at any point (at first glance anyways).

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
atomikscommented, Dec 2, 2022

0.13.0 — there is a small breaking change, but it only requires a 1 prop change. It’s preparing for release now

1reaction
erictaylorcommented, Dec 2, 2022

Trying that change now locally and verified the minified production version comes out right.

const useInsertionEffect = React$m['useInsertionEffect'.toString()],
  useSafeInsertionEffect = useInsertionEffect || ((mn) => mn());
function useEvent(mn) {
  const Cn = react.exports.useRef(() => {});
  return (
    useSafeInsertionEffect(() => {
      Cn.current = mn;
    }),
    react.exports.useCallback(function () {
      for (var Pn = arguments.length, Rn = new Array(Pn), On = 0; On < Pn; On++)
        Rn[On] = arguments[On];
      return Cn.current == null ? void 0 : Cn.current(...Rn);
    }, [])
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Floating-UI] keyboard navigation works weird with react-virtual
I want to integrate react-virtual into Autocomplete, but keyboard ... -ui/packages/react-dom-interactions/src/hooks/useListNavigation.ts.
Read more >
React build creates errors in production - Stack Overflow
This function assumes you have todos in your localStorage which is probably not. const getLocalTodos = () => { setTodos(JSON.parse(localStorage ...
Read more >
useListNavigation - Floating UI
useListNavigation. Adds focus-managed indexed navigation via arrow keys to a list of items within the floating element. import {useListNavigation} from ...
Read more >
@floating-ui/react-dom-interactions - npm
Start using @floating-ui/react-dom-interactions in your project by ... TypeScript icon, indicating that this package has built-in type ...
Read more >
How to use Floating ui with react | by Kyosuke Ito | Dev Genius
Don't forget to add outside div and inside ref, otherwise your code is not working. // App.jsimport { useRef } from "react"; import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found