Skip to content

Fix app crash: React hooks referenced as undefined globals - #6

Draft
thetigerone888 wants to merge 2 commits into
mainfrom
claude/run-6pp617
Draft

Fix app crash: React hooks referenced as undefined globals#6
thetigerone888 wants to merge 2 commits into
mainfrom
claude/run-6pp617

Conversation

@thetigerone888

@thetigerone888 thetigerone888 commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Ran the MARBOHUB POS app (index.html + api.php) end-to-end: served it locally, logged in with the PIN, and drove it with a headless browser.
  • Found that the app throws ReferenceError: useState is not defined immediately after login and never renders past the PIN screen. The bundled app code (#app-src) calls useState/useEffect/useRef/useMemo as bare globals in 34 places, but nothing in index.html ever defines those globals — only window.React and window.ReactDOM exist after the CDN scripts load.
  • Fixed by assigning window.useState = React.useState; (and the other three hooks) right after react-dom finishes loading and before the app script is injected, in loadApp()'s s2.onload callback.

Verification

  • Served index.html/api.php locally with PHP's built-in server and drove it with a headless Chromium (Playwright), logging in with the PIN.
  • Before the fix: blank screen after login, console shows ReferenceError: useState is not defined.
  • After the fix: full UI renders (order-intake bot, and the รับออเดอร์/ออเดอร์/สต๊อก/สรุป tabs) with zero console errors.

Test plan

  • Loaded the app locally, entered the PIN, confirmed the dashboard renders instead of a blank screen
  • Confirmed no console errors after the fix

Generated by Claude Code

Summary by Sourcery

Bug Fixes:

  • Expose React hooks on the window object so bundled app code referencing global hooks no longer throws ReferenceError and the dashboard renders correctly.

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.
@sourcery-ai

sourcery-ai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The 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 load

sequenceDiagram
  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)
Loading

File-Level Changes

Change Details Files
Wire React hook functions onto global window to match how the bundled app code references them.
  • In the ReactDOM onload callback, assign window.useState, window.useEffect, window.useRef, and window.useMemo from the corresponding React hook functions.
  • Ensure these global assignments occur before injecting and executing the bundled app script so that all bare hook calls resolve correctly at runtime.
index.html

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread index.html Outdated
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
window.useState=React.useState;window.useEffect=React.useEffect;window.useRef=React.useRef;window.useMemo=React.useMemo;
Object.assign(window, React);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Applied in 3828285.


Generated by Claude Code

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.
@thetigerone888 thetigerone888 linked an issue Jul 28, 2026 that may be closed by this pull request
@thetigerone888 thetigerone888 self-assigned this Aug 4, 2026
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.

APPSTOCK

2 participants