Skip to content

Fix(useScript): Prevent infinite loops caused by unstable props #1

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

Merged
merged 2 commits into from
Jun 15, 2025

Conversation

olavoparno
Copy link
Member

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.

Describe what's being changed

google-labs-jules bot and others added 2 commits June 14, 2025 04:21
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.
… versions

- Updated `npm-publish.yml` to use `actions/checkout@v4` and `actions/setup-node@v4`, changing the Node.js version to 20.x.
- Updated `pull-request.yml` to use `actions/checkout@v4` and `actions/setup-node@v4`, modifying the Node.js version matrix to include 18.x and 20.x.
@olavoparno olavoparno merged commit 326581d into develop Jun 15, 2025
2 checks passed
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