Skip to content

feat: add React hooks API - #48

Merged
lam0819 merged 1 commit into
mainfrom
agent/sarah-kay-sonnet-5-5/4be706c548c1
Nov 26, 2026
Merged

lam0819 merged 1 commit into
mainfrom
agent/sarah-kay-sonnet-5-5/4be706c548c1

Conversation

@lam0819

@lam0819 lam0819 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds typed React hooks on top of the existing imperative API. It's the follow-up to the competitor review: @kingstinct/react-native-healthkit ships hooks, and until now we only had useForegroundPoll.

Hook value
useHealthConnectAvailability(opts?) 'available' | 'notInstalled' | 'notSupported'
useHealthConnectPermissions(perms, opts?) [status, request]; status.value is { granted, missing, allGranted }
useReadRecords<T>(type, opts?) ReadRecordsResponse<T>
useMostRecentRecord<T>(type, opts?) T | null
useAggregate(type, range, opts?) AggregateResult
useChanges(token, opts?) PullChangesResult

The data hooks return the same { value, satisfied, loading, error, refresh } shape as useForegroundPoll and accept enabled, ready, timeoutMs and intervalMs.

Design notes

  • Foreground refetch: every hook runs on pollOnForeground from src/foreground.ts. Each one fetches on mount and on every resume, and aborts the in-flight fetch when the app goes to the background.
  • No setState after unmount, no stale results: a result is only applied if its poll wasn't aborted. Unmounting or changing arguments aborts the poll, and old data is hidden as soon as the arguments change.
  • Arguments compared by value: the dependency key is JSON.stringify of the arguments, so inline arrays and objects don't cause refetch loops.
  • Range factories: timeRangeFilter / range also accept a function such as () => timeRangeToday(). The function runs on each fetch, so "today" moves forward on resume, and it is left out of the key. Passing timeRangeToday() directly would create a new range on every render. The docs and skills.md warn about this.
  • Single read by default: without ready, each mount or resume does one read. Passing ready turns on polling through the "0 right after resume" gap.
  • request() asks only for the missing permissions, re-checks them, refreshes the hook and resolves with the result.
  • useChanges drains pages with pullChanges, then continues from nextChangesToken on the next resume. It reports each pull through onChanges, which is where the caller persists the token. It doesn't advance past an expired token. A new token prop restarts the sync, and null pauses it.
  • Compatibility: there are no new dependencies, and the RN peer range is unchanged (>=0.84.0).

Docs

  • README: a new "React hooks" usage section and an API reference table.
  • docs/guides.html: a new section, 14 "React hooks", with the nav and TOC updated and later sections renumbered. The meta description now mentions React hooks.
  • docs/api.html: a new "React hooks" section.
  • skills.md: when to use the hooks, a table, an example, and a "do not" for range factories.

Testing

  • src/__tests__/hooks.test.tsx adds 22 tests using React test renderer. They cover the resume refetch, enabled, errors and retry, results arriving after unmount, results for stale arguments, refetch on argument change, range factories, ready polling, request asking only for missing permissions, and useChanges token advance, expiry, reset and unmount.
  • yarn lint, yarn typecheck and yarn test:coverage all pass (253 tests). hooks.ts coverage: 100% lines and 94% branches. Global coverage gates pass.
  • The README dashboard example was type-checked against the real exports.

Add useHealthConnectAvailability, useHealthConnectPermissions,
useReadRecords, useMostRecentRecord, useAggregate and useChanges on top
of the imperative API. Hooks refetch on foreground via pollOnForeground,
compare arguments by value, accept range factories for now-relative
windows, and drop results after unmount or for stale arguments.

Documented in README, docs/guides.html, docs/api.html and skills.md.
@lam0819
lam0819 merged commit bd07fb2 into main Nov 26, 2026
8 checks passed
@lam0819
lam0819 deleted the agent/sarah-kay-sonnet-5-5/4be706c548c1 branch November 26, 2026 15:16
@lam0819 lam0819 mentioned this pull request Nov 26, 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.

1 participant