Fix app crash: React hooks referenced as undefined globals - #6
Fix app crash: React hooks referenced as undefined globals#6thetigerone888 wants to merge 2 commits into
Conversation
The bundled app code calls useState/useEffect/useRef/useMemo as bare globals instead of React.useState etc., but nothing ever defined those globals, so the app threw immediately after the PIN screen and never rendered.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThe PR fixes a crash where React hooks were referenced as undefined globals by explicitly wiring React’s hook functions onto the window object immediately after ReactDOM loads, before the bundled app script is executed. Sequence diagram for React hook globals initialization during app loadsequenceDiagram
actor User
participant Browser
participant index_html
participant ReactDOM_CDN as react_dom_umd
participant AppScript as app_src
User->>Browser: load index_html
Browser->>index_html: execute loadApp
index_html->>Browser: create script s2 (react-dom UMD)
Browser->>ReactDOM_CDN: load react-dom.production.min.js
ReactDOM_CDN-->>Browser: s2.onload
Browser->>Browser: window.useState = React.useState
Browser->>Browser: window.useEffect = React.useEffect
Browser->>Browser: window.useRef = React.useRef
Browser->>Browser: window.useMemo = React.useMemo
Browser->>index_html: create script s3 (app-js) from app-src
index_html->>Browser: appendChild s3
Browser->>AppScript: execute bundled app code (uses global hooks)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Code Review
This pull request updates index.html to expose several React hooks on the global window object before executing the application script. The reviewer suggested a more concise and future-proof approach using Object.assign(window, React) to automatically expose all React APIs, which simplifies the code and prevents manual updates in the future.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| var s2=document.createElement("script");s2.src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"; | ||
| s2.onload=function(){ | ||
| // React loaded → start app | ||
| window.useState=React.useState;window.useEffect=React.useEffect;window.useRef=React.useRef;window.useMemo=React.useMemo; |
There was a problem hiding this comment.
Instead of manually assigning each React hook to the window object, you can use Object.assign(window, React). This is more concise and automatically future-proofs the application in case other React hooks or APIs (such as useCallback, useContext, useLayoutEffect, or Fragment) are used in the future without requiring manual updates to index.html.
| window.useState=React.useState;window.useEffect=React.useEffect;window.useRef=React.useRef;window.useMemo=React.useMemo; | |
| Object.assign(window, React); |
There was a problem hiding this comment.
Addresses review feedback: exposes all of React's exports at once instead of naming each hook individually, so any hook the bundled app code uses (now or later) resolves without further edits here.
Summary
index.html+api.php) end-to-end: served it locally, logged in with the PIN, and drove it with a headless browser.ReferenceError: useState is not definedimmediately after login and never renders past the PIN screen. The bundled app code (#app-src) callsuseState/useEffect/useRef/useMemoas bare globals in 34 places, but nothing inindex.htmlever defines those globals — onlywindow.Reactandwindow.ReactDOMexist after the CDN scripts load.window.useState = React.useState;(and the other three hooks) right afterreact-domfinishes loading and before the app script is injected, inloadApp()'ss2.onloadcallback.Verification
index.html/api.phplocally with PHP's built-in server and drove it with a headless Chromium (Playwright), logging in with the PIN.ReferenceError: useState is not defined.Test plan
Generated by Claude Code
Summary by Sourcery
Bug Fixes: