Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix(useScript): Prevent infinite loops caused by unstable props
The `useScript` hook was susceptible to infinite re-render loops if unstable references (e.g., inline functions or object literals) were passed for `onReady`, `onError`, or `otherProps`. This was because these props were included in the dependency array of the main `useEffect` hook, causing it to re-run on every render if the parent component provided new references. This commit addresses the issue by: 1. Wrapping `onReady`, `onError`, and `otherProps` in `React.useRef`. This ensures that their latest values are accessible within the hook's effects and callbacks without causing re-runs if the underlying values haven't changed. 2. Updating the dependency arrays of `useEffect` and `useCallback` hooks to rely on these stable refs or their `.current` values where appropriate. `handleOnLoad` and `handleOnError` now have empty dependency arrays as they access props via refs. Additionally, test cases have been added in `src/index.test.tsx` to specifically verify that the hook behaves correctly when unstable props are provided, ensuring the script is loaded only once and no infinite loops occur. Both success and error paths for script loading are tested under these conditions.
  • Loading branch information
google-labs-jules[bot] committed Jun 14, 2025
commit 27e171196197a4651c600c9e320bcf1c43dc7cfc
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { preset: 'ts-jest', testEnvironment: 'jsdom', setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'] };
Loading
Loading