Skip to content
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

Introduce useEvent hook #29056

Merged
merged 7 commits into from
Jun 24, 2024
Merged

Introduce useEvent hook #29056

merged 7 commits into from
Jun 24, 2024

Conversation

tsapeta
Copy link
Member

@tsapeta tsapeta commented May 22, 2024

Why

Introducing a hook that fires on event emitter's (e.g. native modules and shared objects) events and returns the event object of the most recently emitted event (or the initial value).

How

Added useEvent hook that uses addListener under the hood and re-renders whenever a new event comes in.

Test Plan

I tried to use the hook and the video player object and the event payload is property typed 👇 and updates whenever the source changes.

Screenshot 2024-06-20 at 11 05 54

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label May 22, 2024
@expo-bot
Copy link
Collaborator

expo-bot commented May 22, 2024

The Pull Request introduced fingerprint changes against the base commit: e7b39a6

Fingerprint diff
[
  {
    "type": "dir",
    "filePath": "../../node_modules/expo",
    "reasons": [
      "bareRncliAutolinking"
    ],
    "hash": "5bc95746701dc74a84664312463960bb73983a8c"
  },
  {
    "type": "dir",
    "filePath": "../../packages/expo",
    "reasons": [
      "expoAutolinkingIos",
      "expoAutolinkingAndroid",
      "expoConfigPlugins",
      "expoConfigPlugins"
    ],
    "hash": "79e5c7ae52bdaf04773a91f31206e2048a36bc6f"
  },
  {
    "type": "dir",
    "filePath": "../../packages/expo-modules-core",
    "reasons": [
      "expoAutolinkingIos",
      "expoAutolinkingAndroid"
    ],
    "hash": "b4603b8089e9877f1b2b7894d03bdf3d443e1b85"
  }
]

Generated by PR labeler 🤖

@tsapeta tsapeta force-pushed the @tsapeta/use-event branch from 6775404 to eabc46a Compare May 28, 2024 13:07
@reichhartd
Copy link
Contributor

Is the hook intended to standardise existing event listener hooks, such as useLowPowerMode in Expo Battery, and make it easier to enable new ones in packages (for example useClipboard)?

@tsapeta
Copy link
Member Author

tsapeta commented May 29, 2024

@reichhartd In the long-term, yes. For now the main purpose is to provide an API for hooks that will be used in shared objects, e.g. the video player from expo-video and the database in expo-sqlite.
Eventually, custom listener wrappers like addClipboardListener will also be replaced by NativeModule.addListener(...). What do you think about that?

@reichhartd
Copy link
Contributor

@tsapeta Yep, sounds good and makes sense to replace the custom listener wrappers with a standardised method.
I asked because I was working on a draft for a function createEventEmitterHook a few weeks ago, and whether a similar function is also scope of this pull request.

@tsapeta tsapeta force-pushed the @tsapeta/use-event branch from eabc46a to f102cfd Compare June 11, 2024 13:27
@tsapeta tsapeta force-pushed the @tsapeta/use-event branch from f102cfd to 6af2fc4 Compare June 20, 2024 09:11
@tsapeta tsapeta marked this pull request as ready for review June 20, 2024 12:08
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Jun 21, 2024
@expo-bot
Copy link
Collaborator

expo-bot commented Jun 21, 2024

📘 Your docs preview website is ready!

tsapeta and others added 2 commits June 21, 2024 10:51
Co-authored-by: Bartosz Kaszubowski <gosimek@gmail.com>

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
tsapeta and others added 2 commits June 21, 2024 12:40
Co-authored-by: Aman Mittal <amandeepmittal@live.com>
@tsapeta tsapeta merged commit 5655df9 into main Jun 24, 2024
12 checks passed
@tsapeta tsapeta deleted the @tsapeta/use-event branch June 24, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants