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

HMR crashes page with React when saving Provider Component #9397

Open
iFreilicht opened this issue Nov 22, 2023 · 4 comments
Open

HMR crashes page with React when saving Provider Component #9397

iFreilicht opened this issue Nov 22, 2023 · 4 comments
Labels
HMR Hot Module Reloading React Fast Refresh

Comments

@iFreilicht
Copy link

🐛 bug report

When I save a file in my project (even if it wasn't changed) that provides a Context and the respective Provider, HMR will try to hot-reload the page and crash it in the process.

The error message is TypeError: _reactDefault.default.useContext(...) is undefined.

image

🎛 Configuration (.babelrc, package.json, cli command)

No babel, I just run

npm exec parcel public/index.html

🤔 Expected Behavior

I would expect the hot reload to work.

😯 Current Behavior

The page crashes with the following error:

TypeError: _reactDefault.default.useContext(...) is undefined
ToastPlayground
src/components/ToastPlayground/ToastPlayground.js:9:29

   6 | import { ToastContext } from "../ToastProvider/ToastProvider";
   7 | 
   8 | function ToastPlayground() {
>  9 |   const { addToast } = React.useContext(ToastContext);
     |                             ^  10 | 
  11 |   return (
  12 |     <div className={styles.wrapper}>

renderWithHooks
node_modules/react-dom/cjs/react-dom.development.js:16305:27
mountIndeterminateComponent
node_modules/react-dom/cjs/react-dom.development.js:20074:12
beginWork
node_modules/react-dom/cjs/react-dom.development.js:21587:15
callCallback
node_modules/react-dom/cjs/react-dom.development.js:4164:13
invokeGuardedCallbackDev
node_modules/react-dom/cjs/react-dom.development.js:4213:15
invokeGuardedCallback
node_modules/react-dom/cjs/react-dom.development.js:4277:30
beginWork$1
node_modules/react-dom/cjs/react-dom.development.js:27451:28
performUnitOfWork
node_modules/react-dom/cjs/react-dom.development.js:26557:11
workLoopSync
node_modules/react-dom/cjs/react-dom.development.js:26466:22
renderRootSync
node_modules/react-dom/cjs/react-dom.development.js:26434:6
performSyncWorkOnRoot
node_modules/react-dom/cjs/react-dom.development.js:26085:34
flushSyncCallbacks
node_modules/react-dom/cjs/react-dom.development.js:12042:21
flushSync
node_modules/react-dom/cjs/react-dom.development.js:26201:6
scheduleRefresh
node_modules/react-dom/cjs/react-dom.development.js:27795:14
.hdge7</performReactRefresh/<
node_modules/react-refresh/cjs/react-refresh-runtime.development.js:300:16
performReactRefresh
node_modules/react-refresh/cjs/react-refresh-runtime.development.js:289:25
.km3Ru</enqueueUpdate<
node_modules/@parcel/transformer-react-refresh-wrap/lib/helpers/helpers.js:33:10
.km3Ru</debounce/<
node_modules/@parcel/transformer-react-refresh-wrap/lib/helpers/helpers.js:20:13
.km3Ru</module.exports.postlude/<
node_modules/@parcel/transformer-react-refresh-wrap/lib/helpers/helpers.js:81:8
.icZzK</hmrAccept/<
node_modules/@parcel/runtime-browser-hmr/lib/runtime-3a0660f0e3b450ae.js:484:34
hmrAccept
node_modules/@parcel/runtime-browser-hmr/lib/runtime-3a0660f0e3b450ae.js:483:32
.icZzK</ws.onmessage
node_modules/@parcel/runtime-browser-hmr/lib/runtime-3a0660f0e3b450ae.js:147:22

💁 Possible Solution

If only I knew 🤷

🔦 Context

I wanted to create a Provider component to avoid prop drilling as part of an exercise.

💻 Code Sample

See https://github.com/iFreilicht/project-toast/tree/parcel-hmr-repro. To quote the README:

To reproduce the issue, run

npm install
npm run dev

The server will start at http://localhost:1234/.
Open that page.

Then, run

touch src/components/ToastProvider/ToastProvider.js

This will crash the page.

🌍 Your Environment

Software Version(s)
Parcel 2.10.3
Node 20.4.0
npm/Yarn npm 9.7.2
Operating System macOS 14.1.1
@mischnic mischnic added HMR Hot Module Reloading React Fast Refresh labels Nov 23, 2023
@k1941996
Copy link

Same with me it, it can break anything. I got an issue with 'react' is not found. Also, HMR does not work when you use useContext.

image
image
image
Modify any code anywhere in the source code, the page refreshes. HMR does not work. Also states get reset. tried preserving the logs. it crashes the code.

@github-actions github-actions bot added the Stale Inactive issues label Jun 11, 2024
@parcel-bundler parcel-bundler deleted a comment from github-actions bot Jun 11, 2024
@mischnic mischnic removed the Stale Inactive issues label Jun 11, 2024
@github-actions github-actions bot added the Stale Inactive issues label Dec 9, 2024
@iFreilicht
Copy link
Author

This is still broken.

@github-actions github-actions bot removed the Stale Inactive issues label Dec 9, 2024
@figureone
Copy link

I'm seeing the same behavior on the same project as the OP, but 100% of the time. npm run dev will report the undefined value from useContext():
Screenshot 2025-01-03 at 3 01 37 PM

Assigning the values returned from createContext() and from useContext() to global variables shows they are different:
Screenshot 2025-01-03 at 3 02 51 PM
https://react.dev/reference/react/useContext#my-component-doesnt-see-the-value-from-my-provider

Here's a branch running parcel and React 19 where you can reproduce:
https://github.com/figureone/project-toast/tree/broken-parcel-usecontext

@figureone
Copy link

Never mind, I'm a dummy and messed up my imports. For reference, the broken useContext() in the comment above was caused because the ToastContext import was pulling the default export instead of a named export, causing the issues. Simple fix is here:
figureone/project-toast@6d118ca

There may still be issues from OP's report, but my problem is solved by the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HMR Hot Module Reloading React Fast Refresh
Projects
None yet
Development

No branches or pull requests

4 participants