Skip to content

fix: resolve conditional React Hook calls in Link and Script shims#223

Open
haddoumounir wants to merge 1 commit intocloudflare:mainfrom
haddoumounir:fix/rules-of-hooks-conditional-calls
Open

fix: resolve conditional React Hook calls in Link and Script shims#223
haddoumounir wants to merge 1 commit intocloudflare:mainfrom
haddoumounir:fix/rules-of-hooks-conditional-calls

Conversation

@haddoumounir
Copy link

Summary

  • link.tsx: The dangerous-scheme early return was placed before all hooks (useState, useRef, useEffect, useCallback, useMemo), making them conditional. Moved the check to a boolean flag before hooks and the early return to after all hooks.
  • script.tsx: The SSR early return (typeof window === "undefined") was before useEffect, making it conditional. Moved useEffect before the SSR check since it never runs during SSR anyway.

Test plan

  • Verify dangerous scheme blocking still works in <Link> (e.g. javascript: hrefs render inert <a>)
  • Verify <Script> SSR rendering still works for beforeInteractive strategy
  • Verify no React warnings about conditional hooks in dev mode
  • Run existing test suite

Move early returns after all hook calls to satisfy the Rules of Hooks.

- link.tsx: the dangerous-scheme early return was before useState/useRef/
  useEffect/useCallback/useMemo — moved it to after all hooks
- script.tsx: the SSR early return was before useEffect — moved useEffect
  before the check since it never runs during SSR anyway
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.

1 participant