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

Fix HMR errors #5209

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Fix HMR errors #5209

wants to merge 3 commits into from

Conversation

dusave
Copy link
Contributor

@dusave dusave commented Nov 1, 2024

Closes #

Changelog

New

  • Add optional chaining checks to prevent undefined references when hot-module reloading

Changed

Removed

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

@dusave dusave self-assigned this Nov 1, 2024
Copy link
Contributor

github-actions bot commented Nov 1, 2024

👋 Hi, this pull request contains changes to the source code that github/github depends on. If you are GitHub staff, we recommend testing these changes with github/github using the integration workflow. Thanks!

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Nov 1, 2024
@github-actions github-actions bot temporarily deployed to storybook-preview-5209 November 1, 2024 16:40 Inactive
Copy link
Contributor

github-actions bot commented Nov 1, 2024

size-limit report 📦

Path Size
packages/react/dist/browser.esm.js 98.6 KB (+0.03% 🔺)
packages/react/dist/browser.umd.js 99.03 KB (+0.15% 🔺)

Copy link

changeset-bot bot commented Nov 1, 2024

🦋 Changeset detected

Latest commit: f3a5ff2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

if (anchorRef.current) {
// Necessary for HMR reloads
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (anchorRef?.current) {
Copy link
Member

@joshblack joshblack Nov 4, 2024

Choose a reason for hiding this comment

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

Just curious, do we know why this ref is undefined when doing our upstream work in dotcom? 🤔 Specifically for HMR. It seems like something is happening with the context value being passed along that's different from how it's being represented in TS?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We think it's because the DOM is being replaced, but honestly that's just a theory 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like @camertron said, we think the DOM is being rebuilt and inserted, which temporarily seems to make the refs undefined. However, it's barely undefined long enough to matter. By the time the page renders, everything is rehydrated and all works fine. But for that split second while it's being swapped out, React throws a fit.

Copy link
Member

Choose a reason for hiding this comment

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

I would have thought the ref would be stable since it ultimately is coming from useRef and the value returned from that wouldn't be undefined between renders 🤔 (although current totally would if the DOM is changing)

Could it be that the context value (MenuContext) is changing during HMR? It seems like we're explicitly casting this value as being defined in:

} = React.useContext(MenuContext) as MandateProps<MenuContextProps, 'anchorRef'>
but with the HMR stuff here that doesn't seem to be the case

@dusave dusave marked this pull request as ready for review November 6, 2024 21:55
@dusave dusave requested a review from a team as a code owner November 6, 2024 21:55
@dusave dusave requested a review from joshblack November 6, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants