Skip to content

Conversation

@Light2Dark
Copy link
Contributor

@Light2Dark Light2Dark commented Nov 23, 2024

📝 Summary

useEventListener wasn't working properly when you passed in ref.current. For example, the ArrowUp & ArrowDown keys to move between cells. This is because changes to ref.current will not trigger a re-render.

🔍 Description of Changes

useEventListener lets you pass in a ref object instead of ref.current and ensure it re-renders correctly when ref.current is no longer null.

Stumbled upon this as I was trying to add new eventListeners. Shoutout to Claude for helping me understand & write 😅.

📋 Checklist

  • I have read the contributor guidelines.
  • For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on Discord, or the community discussions (Please provide a link if applicable).
  • I have added tests for the changes made.
  • I have run the code and verified that it works as expected.

📜 Reviewers

@akshayka OR @mscolnick

@vercel
Copy link

vercel bot commented Nov 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marimo-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 23, 2024 4:31pm
marimo-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 23, 2024 4:31pm

target.removeEventListener(type, eventListener, options);
};
}, [type, target, options]);
}, [type, targetValue, options]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are passing the RefObject, wont this not trigger re-renders when it changes?

Although if this fixes the underlying issues, I trust this is the right change.

Copy link
Contributor Author

@Light2Dark Light2Dark Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutating the ref.current value won't trigger re-renders. But the ref object itself does. I'm basing this because you would normally get a warning when using ref.current in useEffect

Mutable values like 'ref.current' aren't valid dependencies because mutating them doesn't re-render the component. (react-hooks/exhaustive-deps)

But I've searched online and see that a common solution is to use useCallback, but I think that would require a lot of code modification. Do you think I should look more in this direction?

update: we could just use const [element, setElement] = useState(cellRef); and pass in element to the hooks. This is way simpler and has the same effect & logic imo.

Let me revert if I don't find something better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this fixes it, i am totally ok with it! i think this was a bug that was already there, but I added ReactCompiler which memoizes a lot such that subtle bugs like this could crop up.

also, you might have fixed this #2940 (thank you!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this implementation, looks like you are right: https://usehooks-ts.com/react-hook/use-event-listener

and we should be passing in the Ref

@mscolnick
Copy link
Contributor

going to merge this! i can use this for another bugfix, thanks @Light2Dark!

@mscolnick mscolnick merged commit 9768c26 into marimo-team:main Nov 23, 2024
22 of 23 checks passed
@Light2Dark Light2Dark deleted the fix-use-event-listener branch January 15, 2025 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants