fix: ensure click-to-copy buttons are created after hydration#165
Merged
brookslybrand merged 1 commit intoremix-run:mainfrom Jan 22, 2025
rbarbazz:fix-missing-copy-btns
Merged
fix: ensure click-to-copy buttons are created after hydration#165brookslybrand merged 1 commit intoremix-run:mainfrom rbarbazz:fix-missing-copy-btns
brookslybrand merged 1 commit intoremix-run:mainfrom
rbarbazz:fix-missing-copy-btns
Conversation
brookslybrand
approved these changes
Jan 22, 2025
Contributor
brookslybrand
left a comment
There was a problem hiding this comment.
Oh interesting, that's super weird
It's definitely a little less efficient in the sense that the useEffect runs a few extra times. However, it's also definitely better that this works
I throttled CPU perf 20x and everything seems fine. I'm not seeing runaway effects or anything like that. I think this works for now, but if anyone can figure out what this wasn't working in the first place or a better solution, I'm all for that as well
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
I noticed a bug where the code blocks click-to-copy buttons aren't available on the initial page load or after a page refresh.
The buttons became available again after the next navigation event.
Screen.Recording.2025-01-16.at.10.01.04.AM.mov
I couldn't figure out the exact cause of the issue, but I suspect that it has to do with hydration. The markup is overridden after the effect responsible for creating the buttons runs.
In my investigation, I referred to the code for the Remix documentation website (here), which shares a similar implementation, but I am unsure why it's not an issue there.
Solution
Because I couldn't pinpoint the exact problem, I decided to remove the effect dependencies to ensure it runs with every render. Since the effect manipulates the DOM outside of React, it might be a good idea to re-run it every time anyway.
Screen.Recording.2025-01-16.at.10.01.33.AM.mov
I did try using the
useHydratedhook (in the same file) to ensure the hook runs after hydration, but that didn't work.