Skip to content

Throw a descriptive error when React hooks are used outside an AblyProvider - #2316

Merged
ttypic merged 1 commit into
ably:mainfrom
RaphaelFakhri:fix/react-hooks-outside-provider-error
Oct 19, 2026
Merged

ttypic merged 1 commit into
ably:mainfrom
RaphaelFakhri:fix/react-hooks-outside-provider-error

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1849

Summary

Calling useAbly, useChannel or usePresence outside an <AblyProvider> throws a Cannot read properties of undefined TypeError, which does not point to the cause. The hooks now throw Could not find ably client in context. Make sure your ably hooks are called inside an <AblyProvider>.

useAbly checks the context value before it returns the client. useChannelInstance checks the context value before it reads the channel state from it. The same message covers the case where you pass an ablyId that has no matching provider.

Testing

Adds src/platform/react-hooks/src/hooks/useAbly.test.tsx with four cases: useAbly, useChannel and usePresence outside a provider, and useAbly with an unknown ablyId.

npx vitest run src/platform/react-hooks/src/hooks/useAbly.test.tsx

Summary by CodeRabbit

  • Bug Fixes
    • Hooks now show a clear provider error when used outside an AblyProvider or with an unconfigured client, rather than failing while accessing missing context.

@coderabbitai

coderabbitai Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 333fc78f-ea51-42c2-97c2-76ccaeda192c
📥 Commits

Reviewing files that changed from the base of the PR and between 7d076bc and 4607350.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 1a15cc63-3bcb-4112-a5de-9fb717c57dc2

📥 Commits

Reviewing files that changed from the base of the PR and between 35269c8 and 7d076bc.

📒 Files selected for processing (3)
  • src/platform/react-hooks/src/hooks/useAbly.test.tsx
  • src/platform/react-hooks/src/hooks/useAbly.ts
  • src/platform/react-hooks/src/hooks/useChannelInstance.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

The hooks now handle missing Ably context entries with an explicit provider error instead of accessing fields on an absent entry. Tests cover calls outside an <AblyProvider> and a missing requested ID inside a provider.

Changes

React hook context errors

Layer / File(s) Summary
Handle missing context entries
src/platform/react-hooks/src/hooks/useAbly.ts, src/platform/react-hooks/src/hooks/useChannelInstance.ts, src/platform/react-hooks/src/hooks/useAbly.test.tsx
useAbly safely reads the client when its context entry is absent. useChannelInstance throws an <AblyProvider>-related error when its entry is missing. Tests check both cases and verify the expected error.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to 7d076

Missing-provider errors are now actionable, with tests covering absent providers and IDs. No concrete merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: descriptive errors when React hooks run outside an AblyProvider.
Linked Issues check ✅ Passed Issue #1849 requires a descriptive error when hooks run outside an . The PR changes useAbly.ts and useChannelInstance.ts to check for a missing context entry and throw the exact required…
Out of Scope Changes check ✅ Passed The PR changes only the affected hook error handling and adds focused tests for the linked issue behavior. The changes support Issue #1849 and show no unrelated implementation.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the context with care
No provider? The right error is there
A missing ID joins the test
Each hook reports the expected
Then hops through the meadow with flair

Comment @coderabbitai help to get the list of available commands.

@ttypic ttypic 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.

LGTM, thanks for contribution

@ttypic
ttypic force-pushed the fix/react-hooks-outside-provider-error branch from 7d076bc to 4607350 Compare October 19, 2026 12:11
@ttypic
ttypic merged commit f93b51d into ably:main Oct 19, 2026
8 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[React Hooks] wrong error when hooks are called outside <AblyProvider>

2 participants