Skip to content

feat(hooks): add use hook#5126

Open
gaoachao wants to merge 2 commits into
preactjs:mainfrom
gaoachao:p/gcc/use-hook
Open

feat(hooks): add use hook#5126
gaoachao wants to merge 2 commits into
preactjs:mainfrom
gaoachao:p/gcc/use-hook

Conversation

@gaoachao

Copy link
Copy Markdown

Summary

  • add React 19-style use() support in preact/hooks for Promises and Contexts
  • keep use() order-insensitive by avoiding useState/useContext internally
  • expose use through compat default/types and add coverage for Promise, Context, conditional calls, falsy values, rejection, exports, and TS types

Tests

  • npm test

@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

A summary of the benchmark results will show here once they finish.

Results

The full results of your benchmarks will show here once they finish.

tachometer-reporter-action v2 for CI

@gaoachao
gaoachao marked this pull request as ready for review June 29, 2026 08:18
@gaoachao

Copy link
Copy Markdown
Author

Hi @JoviDeCroock @rschristian , I reworked the use() implementation based on your feedback from #4970, especially around keeping it small, avoiding React-specific details in core, and not building it on top of order-sensitive hooks like useState or useContext.

The new PR implements use() for Promises and Contexts, with tests covering conditional calls, falsy promise values, rejection handling, shared promises, Context updates, exports, and TypeScript types. When you have time, could you take another look and let me know if this direction better matches Preact’s design philosophy?

Comment thread hooks/src/index.d.ts
Comment thread hooks/src/index.js Outdated
Comment thread hooks/src/internal.d.ts Outdated
@gaoachao

Copy link
Copy Markdown
Author

@JoviDeCroock Thanks for the suggestions! I applied all three changes:

  • added the TSDoc for use(),
  • inlined the Promise handling into use() to keep the implementation smaller,
  • removed the extra _contexts field and simplified the Context path.

I also reran the relevant hooks tests and the full suite, and everything is passing now.

@gaoachao
gaoachao requested a review from JoviDeCroock July 18, 2026 09:34
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.

2 participants